admin管理员组文章数量:1336660
I’m wondering if you could help with some code I’m struggling with.
I’ve got an ACF Image Field on the Post Category Taxonomy. I need to add it into the post loop so the category image shows up instead of the featured image. Here’s the code that I have in:
$current_term = get_queried_object();
$author_image = get_field('author_image', $current_term );
echo do_shortcode('[image_shortcode id="'.$author_image.'" image_size="original"]');
It’s working on the Category Archive page, but not on the Tags Archive page. Let me know your thoughts.
I’m wondering if you could help with some code I’m struggling with.
I’ve got an ACF Image Field on the Post Category Taxonomy. I need to add it into the post loop so the category image shows up instead of the featured image. Here’s the code that I have in:
$current_term = get_queried_object();
$author_image = get_field('author_image', $current_term );
echo do_shortcode('[image_shortcode id="'.$author_image.'" image_size="original"]');
It’s working on the Category Archive page, but not on the Tags Archive page. Let me know your thoughts.
Share Improve this question asked May 15, 2020 at 17:30 JasonJason 451 silver badge9 bronze badges2 Answers
Reset to default 0If you're on a tags archive would the $current_term
not be the tag? rather than the category? a post can be in multiple categories so which category should it pull the image from? or are you associating images with each tag as well as each category and you want the image associated with that tag?
Presuming you want the post to pull through the image from one of the categories it is associated with you either need a way of setting a primary category (I think the Yoast SEO plugin adds this) or you need to have a rule... like you just use the first category in the list. Then you will need to query the category using the post ID to get the object... untested but something like this...
$post_categories = wp_get_post_categories($post_id, array('fields' => 'all'));
$current_term = $post_categories[0];
$author_image = get_field('author_image', $current_term );
echo do_shortcode('[image_shortcode id="'.$author_image.'" image_size="original"]');
Thanks for your help Bob. I appreciate it. Your answer got me close, but I was able to figure this out. The actual answer ended up being this:
$post_categories = wp_get_post_categories( get_the_ID(), array('fields' => 'all'));
if ( $post_categories ) {
$current_term = $post_categories[0];
$author_image = get_field('author_image', $current_term );
echo do_shortcode('[image_shortcode id="'.$author_image.'" image_size="original"]');}
Thanks again.
本文标签: loopUse ACF Category Image for all Taxonomy Archive Views
版权声明:本文标题:loop - Use ACF Category Image for all Taxonomy Archive Views 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742421850a2471818.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论