Display WordPress featured image, thumbnail, or YouTube thumb

WordPress Tips & Tricks
by
Michelle McGinnis
 | 

Huge thanks to Vladimir Prelovac, who wrote the original function. I use it a ton and have adapted it a bit to check for:

  1. Featured image; then
  2. First image attached to the post; then
  3. The first image inserted in the post; then
  4. The thumbnail of the first YouTube video inserted in the post

All I added was the featured image bit. :)

Here’s the full function, which should be inserted in your theme’s functions.php file: [UPDATED 2011.08.26 to scan for youtu.be URLs as well as the traditional youtube.com URL]

// Note that your theme must support post thumbnails for this function to work.
// If you are getting an error try adding add_theme_support('post-thumbnails'); to your functions. php file
function vp_get_thumb_url($text, $size){
    global $post;
    $imageurl="";

    // Check to see which image is set as "Featured Image"
    $featuredimg = get_post_thumbnail_id($post->ID);
    // Get source for featured image
    $img_src = wp_get_attachment_image_src($featuredimg, $size);
    // Set $imageurl to Featured Image
    $imageurl=$img_src[0];

    // If there is no "Featured Image" set, move on and get the first image attached to the post
    if (!$imageurl) {
        // Extract the thumbnail from the first attached imaged
        $allimages =&get_children('post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );

        foreach ($allimages as $img){
            $img_src = wp_get_attachment_image_src($img->ID, $size);
            break;
        }
        // Set $imageurl to first attached image
        $imageurl=$img_src[0];
    }

    // If there is no image attached to the post, look for anything that looks like an image and get that
    if (!$imageurl) {
        preg_match('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*)/i' ,  $text, $matches);
        $imageurl=$matches[1];
    }

    // If there's no image attached or inserted in the post, look for a YouTube video
    if (!$imageurl){
        // look for traditional youtube.com url from address bar
        preg_match("/([a-zA-Z0-9\-\_]+\.|)youtube\.com\/watch(\?v\=|\/v\/)([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $text, $matches2);
        $youtubeurl = $matches2[0];
        $videokey = $matches2[3];
    if (!$youtubeurl) {
        // look for youtu.be 'embed' url
        preg_match("/([a-zA-Z0-9\-\_]+\.|)youtu\.be\/([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $text, $matches2);
        $youtubeurl = $matches2[0];
        $videokey = $matches2[2];
    }
    if ($youtubeurl)
        // Get the thumbnail YouTube automatically generates
        // '0' is the biggest version, use 1 2 or 3 for smaller versions
        $imageurl = "http://i.ytimg.com/vi/{$videokey}/0.jpg";
    }

    // Spit out the image path
    return $imageurl;
}

Put the following in the Loop wherever you’d like the image to display (wrapped in open/close php tags).

// Show featured image, or first image if no featured, or YouTube thumbnail
if (function_exists('vp_get_thumb_url')) {

// Set the desired image size. Swap out 'thumbnail' for 'medium', 'large', or custom size
$thumb=vp_get_thumb_url($post->post_content, 'thumbnail');

if ($thumb!='') { ?>

<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><img class="alignleft" src="<?php echo $thumb; ?>" alt="<?php get_the_title(); ?>" /></a>

<?php }
}

4 Responses to Display WordPress featured image, thumbnail, or YouTube thumb

  1. Michael says:

    I was to "Lazy" to adapt Vladimir Prelovac's function this morning , as I was reading through the comments I stumbled on your comment … exactly what I needed ::: THX so much ;)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>