1

I have added an image custom field for categories, in which I assign an image for every category. Something like this:

enter image description here

I have a custom post type, and for each post I publish in this post type, I assign one category. I need each post I publish to change its featured image, according to the category assigned to it, with the image that has been added to the custom field.

Any help?

Edit:

I found this topic about the same question:

https://support.advancedcustomfields.com/forums/topic/post-featured-image-from-category-custom-field-image/

There's an explanation on how to do it. Hopefully, someone can help me with the code since I'm not skilled enough to be able to do it.

1

You could filter the the_post_thumbnail() function, which will dynamically show the assigned category image across all your custom post type, rather than using acf_save_post to save the category image in the post featured image meta field.

By filtering the the_post_thumbnail() for your specific post type, this means if you change the image on category in the future, it will automatically update all the custom post type featured images with the assigned category.

Here is rough example that might get you on the right track, read my comments in code carefully so you can update the relevant fields to suit you environment...

/**
 * @param $html
 * @param $post_id
 * @param $post_thumbnail_id
 * @param $size
 * @param array $attr
 * @return string $html
 */
function modify_cars_featured_img_html($html, $post_id, $post_thumbnail_id, $size, $attr) {

    // if post type is not 'cars' then return html now
    if(get_post_type($post_id) <> 'cars') return $html;

    // get the categories from cars post
    $cat = get_the_terms($post_id,'category');

    // if categories var is array then return categories else false
    $cat = is_array($cat) ? $cat : false;

    // if categories is false then return html now
    if(!isset($cat[0])) return $html;

    // get categories image acf field using first existing category id in array objects
    $id = get_field('your_category_acf_img_field_name','category_'.$cat[0]->term_id);
    
    // get the attachment data based on passed size and category image id
    $src = wp_get_attachment_image_src($id, $size);

    // get the media item image title from category image id
    $alt = get_the_title($id); 

    // if class is passed in post thumbnail function in theme make sure we pass this to featured image html
    $class = isset($attr['class']) ? $attr['class'] : false;

    // the new post thumbnail featured image html
    $html = '<img src="' . $src[0] . '" alt="' . $alt . '" ' . ( $class ? 'class="' . $class . '"' : null ) . ' />';
    
    // return the image html
    return $html;
}

// add the filter
add_filter('post_thumbnail_html', 'modify_cars_featured_img_html', 99, 5);

Add all this updated code to your functions.php.


Updated code above to return $html early at two points in this function, as I was originally only returning which was causing your other post thumbnails to break.

Make sure you also set your categories image acf field to return image ID or this wont code wont work.

enter image description here

Let me know if this fixes it.

| improve this answer | |
  • I'm sorry, it didn't work. I couldn't find out why. – L. core Jul 23 at 23:51
  • Did you change the field name – joshmoto Jul 24 at 8:47
  • Yes, the post type and the field name, but nothing changed. – L. core Jul 24 at 13:31
  • Sweet are you using a custom category taxonomy for your custom post type? Where it says ‘category_’ in your image field name you need to change that your custom taxonomy name – joshmoto Jul 24 at 14:01
  • No, I'm using wordpress category, ‘category_’ is correct. – L. core Jul 24 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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