Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
"but without success" is no proper problem description. Please add more information. –  kaiser Sep 12 '12 at 10:30
    
When featured image is missing nothing is displayed. Thank you. –  daniel.tosaba Sep 12 '12 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 Sep 12 '12 at 16:54

3 Answers 3

up vote 4 down vote accepted

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;
}
share|improve this answer
    
Please take a look at above post and tell me what am I doing wrong? –  daniel.tosaba Sep 12 '12 at 4:01
1  
Thats perfect. Love it –  Foxinni May 28 at 8:15

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 );
share|improve this answer
    
Please take a look at above post and tell me what am I doing wrong? –  daniel.tosaba Sep 12 '12 at 3:59

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' ));
        }

    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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