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 would like to create a condition that checks to see if a post has a thumbail and if it does display it, otherwise display the first image in a post.

I tried something like this in my loop.php but it didn't seem to work:

<?php if (has_post_thumbnail()) { ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(640,320)); ?></a>
<?php } else { ?>
<a href="<?php the_permalink(); ?>"><img src="<?php echo catch_that_image(); ?>" /></a>
<?php } ?>

This goes in my functions.php file:

<?php
    function catch_that_image() {
        global $post, $posts;
        $first_img = '';
        ob_start();
        ob_end_clean();
        $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
        $first_img = $matches [1] [0];

        // no image found display default image instead
        if(empty($first_img)){
             $first_img = get_bloginfo('template_url')."/images/no_image.gif";
        }
            return $first_img;
    }

    $imgURL = catch_that_image();
?>
share|improve this question
add comment

2 Answers

Get the Image does what you need and better. It's NOT overwhelming with lots of unnecessary features, and does what it says. Try it to see if it does what you need.

How does Get the Image plugin pull images?

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

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

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

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

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

share|improve this answer
    
I installed this plugin and activated it. I don't understand how I can configure it. It doesn't work on my site.. –  Brian Jul 2 '12 at 3:03
    
@Matt Justin Tadlock's plugins come with a readme.html file, follow the instructions in it. Never seen an updated plugin by Justin that didn't work (so to say). :) –  its_me Jul 2 '12 at 3:10
    
let me take a look at that...thank you :) –  Brian Jul 2 '12 at 3:12
    
+1 for Justin's plugin, wonderfully written one! –  brasofilo Jul 2 '12 at 6:57
1  
@Matt Why is that a problem? Are you not using a caching plugin? If you are not, you have more serious problems to deal with, than this neat code. The plugin does the checks in a very sensible manner. –  its_me Jul 3 '12 at 14:50
show 1 more comment

This should do it, I'm using it and it's super easy and simple, just paste this into your functions.php:

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);// the size of the thumbnail is defined in a function above
            }
        }
    }
}  //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');
share|improve this answer
add comment

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.