最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - ACF under category name in shop page

programmeradmin1浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

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.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

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.

Share Improve this question asked Jun 27, 2019 at 20:15 Łukasz GręźlikowskiŁukasz Gręźlikowski 254 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论