3

I am using this big theme that is almost entirely relying on widgets and sliders. Now I trying to avoid looking for post_thumbnail output code in all the included files in order to achieve fallback which should then grab first image in post or at last display default image.

Is there a way of controlling this via functions file?

Any help appreciated.

Solution based on post_thumbnail_html filter hook doesn't display featured image if it is not explicitly set:

add_filter( 'post_thumbnail_html', 'my_post_thumbnail_fallback', 20, 5 );
function my_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
    if ( empty( $html ) ) {
        $image = get_children( "post_parent={$post_id}&post_type=attachment&post_mime_type=image&numberposts=1" );
        if($image){
                foreach ($image as $attachment_id => $attachment) {
                        $src = wp_get_attachment_image_src($attachment_id);
                 return printf(
                                 '<img src="%s" height="%s" width="%s" />'
                                ,$src[0]
                                ,get_option( 'thumbnail_size_w' )
                                ,get_option( 'thumbnail_size_h' )
                                );
            }
         } 
         else {
               return printf(
                                 '<img src="%s" height="%s" width="%s" />'
                                ,get_template_directory_uri().'/images/featured/featured.jpg'
                                ,get_option( 'thumbnail_size_w' )
                                ,get_option( 'thumbnail_size_h' )
                                );
         }  
    } 
        return $html;
}

Another solution that I found in this article and it's based on different action hooks does thing as intended which displays(sets) first attachment image in post as featured or in last case scenario displays(sets) default image as post's featured image:

function autoset_featured() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID);
              if (!$already_has_thumb)  {
              $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                          if ($attached_image) {
                                foreach ($attached_image as $attachment_id => $attachment) {
                                set_post_thumbnail($post->ID, $attachment_id);
                                }
                           } else {
                                set_post_thumbnail($post->ID, '414');
                           }
                        }
      }  //end function
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');

Now.. I do like that post_thumbnail_html filter hook solution and am intrigued by it not working.

Any help appreciated.

3
  • "but without success" is no proper problem description. Please add more information.
    – kaiser
    Commented Sep 12, 2012 at 10:30
  • When featured image is missing nothing is displayed. Thank you. Commented Sep 12, 2012 at 15:22
  • 1
    Please always add such infos to the question, as not all people read comments - we just got too many questions/answers :)
    – kaiser
    Commented Sep 12, 2012 at 16:54

3 Answers 3

5

you can use the post_thumbnail_html filter hook which passes 5 variables to your hooked function :

  • $html - the Output html of the post thumbnail
  • $post_id - the post ID.
  • $post_thumbnail_id - the attachment id of the image
  • $size - the size requested or default
  • $attr - Query string or array of attributes.

So something like:

add_filter( 'post_thumbnail_html', 'my_post_thumbnail_fallback', 20, 5 );
function my_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
    if ( empty( $html ) ) {
        // return you fallback image either from post of default as html img tag.
    }
    return $html;
}
2
  • Please take a look at above post and tell me what am I doing wrong? Commented Sep 12, 2012 at 4:01
  • 1
    Thats perfect. Love it
    – Foxinni
    Commented May 28, 2014 at 8:15
4

As an addition to @Bainternet answer:

Retrieve the set size from the options table. This means, the returned image will align with all user settings. Calls the default thumb only from the template directory to get around cases where child themes are present and won't have the thumbnail.

/**
 * Default post thumbnail image.
 * 
 * @param  string $html The Output HTML of the post thumbnail
 * @param  int $post_id The post ID
 * @param  int $post_thumbnail_id The attachment id of the image
 * @param  string $size The size requested or default
 * @param  mixed string/array $attr Query string or array of attributes
 * @return string $html the Output HTML of the post thumbnail
 */
function wpse64763_post_thumbnail_fb( $html, $post_id, $post_thumbnail_id, $size, $attr )
{
    if ( empty( $html ) )
    {
        return sprintf(
            '<img src="%s" height="%s" width="%s" />',
            get_template_directory_uri().'/path/to/default-thumb.png',
            get_option( 'thumbnail_size_w' ),
            get_option( 'thumbnail_size_h' )
        );
    }

    return $html;
}
add_filter( 'post_thumbnail_html', 'wpse64763_post_thumbnail_fb', 20, 5 );
1
  • Please take a look at above post and tell me what am I doing wrong? Commented Sep 12, 2012 at 3:59
0

There you go, just tested and like this work fine for me ;)

    add_filter( 'post_thumbnail_html', 'wc_post_thumbnail_fallback', 20, 5 );
function wc_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
   if ($html) {
        return $html;
    }else {
        $args = array(
            'numberposts' => 1,
            'order' => 'ASC',
            'post_mime_type' => 'image',
            'post_parent' => $post_id,
            'post_status' => null,
            'post_type' => 'attachment',
        );

        $images = get_children($args);

        if ($images) {
            foreach ($images as $image) {
                return wp_get_attachment_image($image->ID, $size);
            }
        }else{
            printf('<img src="%s" height="%s" width="%s" />'
                ,get_template_directory_uri().'/images/featured/featured.jpg'
                ,get_option( 'thumbnail_size_w' )
                ,get_option( 'thumbnail_size_h' ));
        }

    }
}

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.