0

I'm using display categories only for shop page setup in woocommerce. I added custom field (textfield) for product categories in Advanced custom fields.

I would like to display this text under category title on shop page (not on category page).

in content-product_cat.php after

do_action( 'woocommerce_shop_loop_subcategory_title', $category );

I added:

$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
$custom_field = get_field('krotki_opis_kategorii', $post_id); // My Advanced Custom Field Variable
echo $custom_field;

But it's not working.

1 Answer 1

0

If i understand you correctly here you want to display some text under each of the category titles on your shop page. Instead of editing the template i would suggest using a hook. To do this you should move your code into functions.php.

The complete code would look something like this:

add_action('woocommerce_after_subcategory_title', 'wpse_add_custom_text_under_category_title', 10);

function wpse_add_custom_text_under_category_title($category) {
   $term_id = 'product_cat_'.$category->term_id;
   the_field('krotki_opis_kategorii', $term_id);
}

The reason why your code is not working is because when you run get_queried_object_id on the shop page it will return the id of the page and not the category. When you use the hook, the $category object will be passed in through the hook like in the code above.

Hope this was what you were looking for. I did not test this code but it should work.

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.