5

I'm hoping that I can explain what I'm going for here. On my current project's main page, I'm displaying each post's Featured Image Thumb, along with an excerpt of the post's content. In each of the actual post pages, there is a default WP gallery with 2 to 4 images inside.

What I want to do, is make it so that my client doesn't always have to use a Featured Image in each post, so that the thumbnail always shows up on the home page. In other words, right now, he has to pick an image to be Featured in order for there to be that representative thumbnail for each post in the loop on the home page.

Can I make it so that even if he doesn't pick a featured image for a post, there still is a thumbnail to represent the post? Can I make it automatically just pick the first image in the post's gallery, if no featured image is chosen?

Just in case, here is some of what I'm using, code wise:

              <div id="image-wrap">
             <?php 
              if ( has_post_thumbnail() ) {
              the_post_thumbnail();
               } 
              ?>




         </div><!--end image-wrap-->


                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                 <?php the_title('<h2 class="entry-title"><a href="' .  

get_permalink() . '" title="' . the_title_attribute('echo=0') . '" 
rel="bookmark">', '</a></h2>'); ?>

                 <div class="entry-content">
<?php the_content(__('Continue reading', 'example')); ?>
<?php wp_link_pages('before=<p class="pages">' . __('Pages:','example') .

'&after=</p>'); ?>
</div>





</div>

<?php endwhile; ?>

<?php else : ?>

<p class="no-posts"><?php _e('Sorry, no posts matched your criteria',  
'example'); ?></p>

<?php endif; ?>
<?php wp_reset_query(); ?> 

Then this is in my FUNCTIONS File:

 // This theme uses post thumbnails
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 150, 100, true );




// Automatically makes featured image thumbs a clickable link
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );

function my_post_image_html( $html, $post_id, $post_image_id ) {

$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr(   
get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>';
return $html;

}

// This theme displays full size featured image on the Post's page
function InsertFeaturedImage($content) {

global $post;

$original_content = $content;

if ( current_theme_supports( 'post-thumbnails' ) ) {

    if ((is_page()) || (is_single())) {
        $content = the_post_thumbnail('page-single');
        $content .= $original_content;

    }

}
return $content;
}

add_filter( 'the_content', 'InsertFeaturedImage' ); 

http://dependablecarcompany.com is the address if you care to see what I'm talking about. You'll see what I mean when looking at the post titled: "1991 GMC Sierra". I did not use a featured image for the post, so there is no thumbnail shown. Thanks in advance!

2
  • Sorry, won't let me edit that last comment Ok, so with some help, I was able to get even closer to what I'm going for with: if ( has_post_thumbnail() ) { the_post_thumbnail(); } else { get_the_image( array('size' => 'thumbnail', 'image_class' => 'wp-post-image')); } The thumbnail that is output is bit off in the size, but I think I can figure that part out. Commented Jul 7, 2012 at 21:12
  • Related: wordpress.stackexchange.com/questions/45529/… Commented Jul 30, 2023 at 23:19

2 Answers 2

6

Can't recommend Get the Image plugin (by Justin Tadlock) enough. It's among the best out there, very well maintained and neat code — more importantly, it does what you need, while giving you a whole slew of options.

How does it pull images?

  1. Looks for an image by custom field (one of your choosing).

  2. If no image is added by custom field, check for an image using the_post_thumbnail() (WP 2.9's new image feature).

  3. If no image is found, it grabs an image attached to your post.

  4. If no image is attached, it can extract an image from your post content (off by default).

  5. If no image is found at this point, it will default to an image you set (not set by default).

9
  • Ok, well I'm kind of getting there with this, but now I'm running into problems with this. I've used this to show a thumbnail if there is no Featured Image specified: if ( has_post_thumbnail() ) { the_post_thumbnail(); } else { get_the_image( array('size' => 'thumbnail')); } What is odd though,is that the image output isn't styled like the rest of my thumbs. If you look on dependablecarcompany.com Commented Jul 6, 2012 at 23:27
  • Also, now if you click on the post in question: "1991 GMC Sierra" you'll see that the main image is now not being shown. Without this plugin and the rewritten way of calling the thumbnail in the loop, the post's main image is displayed right at the top middle of the page. You can see what I'm talking about if you click any other post. Commented Jul 6, 2012 at 23:32
  • Okay, the way this plugin works, it may not be a great fit for your theme as it is, unless you'd want to modify your code. But did you read the readme.html file that came with the plugin?
    – its_me
    Commented Jul 7, 2012 at 1:34
  • 2
    @AnthonyMyers I'd avoid TimThumb (and the like) if I were you. And I believe that would be the suggestion from anyone on WPSE as well.
    – its_me
    Commented Jul 7, 2012 at 16:38
  • 1
    @AnthonyMyers Just started a bounty on your question You'll get help. My rep is of no use to me anyway. :)
    – its_me
    Commented Jul 7, 2012 at 16:40
6
+100

Just check for the thumbnail, and if it's not set, use the first image from the gallery instead. Something like this:

$size = 'thumbnail'; // whatever size you want
if ( has_post_thumbnail() ) {
    the_post_thumbnail( $size );
} else {
    $attachments = get_children( array(
        'post_parent' => get_the_ID(),
        'post_status' => 'inherit',
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'order' => 'ASC',
        'orderby' => 'menu_order ID',
        'numberposts' => 1)
    );
    foreach ( $attachments as $thumb_id => $attachment ) {
        echo wp_get_attachment_image($thumb_id, $size);
    }
}

Basically, if no featured image exists, then has_post_thumbnail() will return false. So you call get_children to get the attached images for this post. Note numberposts = 1, so it only gets the first one. Then you output that image using wp_get_attachment_image.

Note that I used the foreach even though I'm only getting 1 image here. This is because get_children returns an array of posts, regardless of how many posts it's returning. so I'm "looping" through an array of size 1. If there's no images, the array will be empty and so nothing is output.

If you don't like using get_children, then a similar new WP_Query could be constructed to get the first attachment image in a similar manner.

  • Added the missing curly brace to the foreach loop.
4
  • Awesome. Forgive me, I'm terrible at php, so this is an easy fix I'm sure, but I'm getting a syntax error when trying to use this in dreamweaver. (Yes, I know, I shouldn't use DW, haha!) Commented Jul 7, 2012 at 21:19
  • Umm.. Nope. No syntax errors there. Check your typing?
    – Otto
    Commented Jul 7, 2012 at 21:37
  • Ok, I got this method to work, but it outputs a thumbnail that is un-styled and is and stretched out/pixelated. Thank you for your response though! Commented Jul 8, 2012 at 5:04
  • I would rather use the code you suggested, instead of a plugin, but it just won't output the thumbnail right. But anway, would you happen to be able to help with my related question on this issue? wordpress.stackexchange.com/questions/57861/… Commented Jul 10, 2012 at 0:47

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.