loop - Use ACF Category Image for all Taxonomy Archive Views

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 Taxo

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 badges
Add a comment  | 

2 Answers 2

Reset to default 0

If 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742421850a4440843.html

相关推荐

  • loop - Use ACF Category Image for all Taxonomy Archive Views

    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 Taxo

    7小时前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信