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

本文标签: loopUse ACF Category Image for all Taxonomy Archive Views