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.

2 Answers 2

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.

10
  • I'm sorry, it didn't work. I couldn't find out why.
    – L. core
    Commented Jul 23, 2020 at 23:51
  • Did you change the field name
    – joshmoto
    Commented Jul 24, 2020 at 8:47
  • Yes, the post type and the field name, but nothing changed.
    – L. core
    Commented Jul 24, 2020 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
    Commented Jul 24, 2020 at 14:01
  • No, I'm using wordpress category, ‘category_’ is correct.
    – L. core
    Commented Jul 24, 2020 at 14:31
0

A long time ago I used code from here: https://wpforce.com/automatically-set-the-featured-image-in-wordpress/ (Looks like the post does not longer exist) and https://wpsites.net/web-design/add-default-featured-image-for-each-post-in-a-category/

The category part works but the set first image in post as featured does not.

// Inside your functions file add the following code
// 
function wpforce_featured() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID); // If post have a featured image use that.
              if (!$already_has_thumb)  {
              // If post does not have a featured image then get the first post image and set as featured image.
              $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); // Number 1 relates to taking post image number 1 and adding it as a featured image.
                          if ($attached_image) {
                                foreach ($attached_image as $attachment_id => $attachment) {
                                set_post_thumbnail($post->ID, $attachment_id);
                               // $attachment_id = attachment_url_to_postid( $image_url );
                               //    echo $attachment_id;
                               
                                }
                           } else if ( in_category('WordPress') ){ // Add your own categories.
                                set_post_thumbnail($post->ID, '42'); 
                                // Find attachment media id by right clicking the image in the Media library and selecting inspect element. Look for the data-id number. This number is then added to the post id. Or change to list view and look bottom left in the status bar for an ID number.
                           }
                           
                           else if ( in_category('dog') ) {
                           set_post_thumbnail($post->ID, '39'); //48
                           }
                           else if ( in_category('cat') ) {
                           set_post_thumbnail($post->ID, '45'); //44
                           }
                           else if ( in_category('Uncategorized') ) {
                           set_post_thumbnail($post->ID, '49'); //40
                           }
                        }
      }  //end function
add_action('the_post', 'wpforce_featured');
add_action('save_post', 'wpforce_featured');
add_action('draft_to_publish', 'wpforce_featured');
add_action('new_to_publish', 'wpforce_featured');
add_action('pending_to_publish', 'wpforce_featured');
add_action('future_to_publish', 'wpforce_featured');

Your Answer

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

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