Home › Forums › General Issues › Displaying custom field on Woocommerce /shop page
Hi there,
I’ve been trying to add a second custom field to my products that will display on the cateogry or /shop pages. I can get my first field to display properly, but when I add a second field it just repeats the content of the first one (despite changing the field ID). I’m new to PHP so I’m definitely still learning. What am I doing wrong?
Added to functions.php:
/* Register Supplier Custom Field */
if( function_exists('supplier') ):
acf_add_local_field_group(array(
'key' => 'group_1',
'title' => 'My Group',
'fields' => array (
array (
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => 'text',
)
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
));
endif;
/* Display Supplier Field */
function skyverge_shop_display_post_meta() {
global $product;
// replace the custom field name with your own
$supplier = get_post_meta( $product->id, 'supplier', true );
// (optional) cleans up custom field names: replace underscores with spaces
$supplier = str_replace( '_', ' ', $supplier );
// Add these fields to the shop loop if set
if ( ! empty( $supplier ) ) {
echo '<div class="product-meta"><span class="product-meta-title custom-field-supplier"></span> ' . ucwords( $supplier ) . '</div>';
}
}
add_action( 'woocommerce_before_shop_loop_item_title', 'skyverge_shop_display_post_meta', 35 );
/* Register Info Custom Field */
if( function_exists('info') ):
acf_add_local_field_group(array(
'key' => 'group_2',
'title' => 'My Group',
'fields' => array (
array (
'key' => 'field_2',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => 'text',
)
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
));
endif;
/* Display Info Field */
function skyverge_shop_display_post_meta2() {
global $product;
// replace the custom field name with your own
$info = get_post_meta( $product->id, 'info', true );
// Add these fields to the shop loop if set
if ( ! empty( $info ) ) {
echo '<div class="product-meta"><span class="product-meta-title custom-field-product_info"></span> ' . ucwords( $info ) . '</div>';
}
}
add_action( 'woocommerce_after_shop_loop_item_title', 'skyverge_shop_display_post_meta', 3 );
To clarify: the “supplier” content fills both the Supplier and Info spots.
Your fields have the same name “sub_title”. You are not creating 2 different fields and only one value can be stored. Fields placed on the same WP object (Post/Term/User) must have unique names.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.