Any easy way to automatically set the first inline image in a post as the thumbnail?

Is there a plugin for this? Has anyone done it before?

Related posts

Leave a Reply

3 comments

  1. You may find the answer in this tutorial : How to Set a Default Fallback Image for WordPress Post Thumbnails

    Add this in functions.php in your theme folder :

    add_theme_support( 'post-thumbnails' );
    
    function myprefix_main_image() {
        $attachments = get_children( 'post_parent='.$post->ID.'&post_type=attachment&post_mime_type=image&order=desc' );
        if( $attachments ) {
            $keys = array_reverse( $attachments );
            set_post_thumbnail( $post->ID, $keys[0]->ID );
        }; 
    }
    

    And this in your template, where you want to show the post image :

    <?php if ( (function_exists( 'has_post_thumbnail') ) && ( has_post_thumbnail() ) ) {
      echo get_the_post_thumbnail( $post->ID );
    } else {
       myprefix_main_image();
       echo get_the_post_thumbnail( $post->ID );
    } ?>
    

    EDIT : much better, thanks Chip Bennett

  2. Get The Image is great plugin for related functionality. You will need to configure funciton call a bit, but it will be able both to scan for image and save it into thumbnail for you. Was writing quickly and messed up a bit. What it can do is save found image into custom field (which might or might not be what you want). If you want to actually make featured image out of it – that is going to be considerably more complex.