0

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.

2 Answers 2

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"]');
4
  • I tried this and it didn't seem to work. I'm trying to always show the ACF category image in the loop no matter what page it's on (for category archive, tag archive, search, etc).
    – Jason
    May 16, 2020 at 20:54
  • Posts can be in multiple categories, use var_dump() to help you figure out where the code above is going wrong, you need to get the category term object in order for get_field() to do what you want.
    – Bob
    May 16, 2020 at 20:59
  • I'm just trying to always have it show the category ACF image no matter where the post is found. All posts will always ONLY have one category set. I can't seem to find anywhere to help with always showing the category ACF image. I'm not familiar with var_dump(), but if you have any insight to update the code on how to do this it would be helpful. Thanks.
    – Jason
    May 17, 2020 at 19:03
  • var_dump() allows you to inspect the contents of a variable by echoing it out for example var_dump($current_term); would show you the contents of $current_term so you can figure out where things a going wrong. Have you tried this plugin? wordpress.org/plugins/category-featured-image it looks like it works by overwriting the featured image when the post is saved.
    – Bob
    May 18, 2020 at 11:34
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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