Here is a quick function to extract the thumbnail image from the post. This is quite useful especially on older themes or if you are not using post thumbnail functionality of newer WordPress versions.
The function can be called within the loop. It will scan for image attachments and return the URL to thumbnail of the first image.
If no attachments are found, it would return the URL of the first image found (for example an externally linked image).
If no images are found, but a youtube video is embedded, it would return the thumbnail of the youtube video.
function vp_get_thumb_url($text) { global $post; $imageurl=""; // extract the thumbnail from 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); break; } $imageurl=$img_src[0]; // try to get any image if (!$imageurl) { preg_match('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*)/i' , $text, $matches); $imageurl=$matches[1]; } // try to get youtube video thumbnail if (!$imageurl) { preg_match("/([a-zA-Z0-9\-\_]+\.|)youtube\.com\/watch(\?v\=|\/v\/)([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $text, $matches2); $youtubeurl = $matches2[0]; if ($youtubeurl) $imageurl = "http://i.ytimg.com/vi/{$matches2[3]}/1.jpg"; else preg_match("/([a-zA-Z0-9\-\_]+\.|)youtube\.com\/(v\/)([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $text, $matches2); $youtubeurl = $matches2[0]; if ($youtubeurl) $imageurl = "http://i.ytimg.com/vi/{$matches2[3]}/1.jpg"; } return $imageurl; }
Here is example usage within the home page loop to show the post thumbnail in the excerpt (this is actually used on my homepage).
global $post; $thumb=vp_get_thumb_url($post->post_content); if ($thumb!='') echo '<img style=" width:150px;" src="'.$thumb.'" alt="'. get_the_title().'" />';
Suggested reading:
- How to get images from posts (and videos)
- Check if the post is older than X days before placing ads
- Adding boxes to WordPress 2.5+ Write Post panel
Posted in: WordPress
TAGS:excerpt thumbnails wordpress, function video youtube wordpress, get youtube thumbnail wordpress, post thumbnail plugin wordpress, recent comments wordpress thumbnail, simple wordpress, thumbnail wordpress, wordpress function image, wordpress scan image, wordpress simple post thumb, wordpress simple thumbs, wordpress thumbnail, wordpress thumbnail seo theme, wordpress thumbnail youtube video, youtube thumbnail wordpres, youtube wordpress thumbnails






23 Comments
Hi Vladimir,
I'm publishing YouTube videos using a custom field named 'videourl'.
Any idea of how to extract the video thumbnail image from that custom field?
Thank you sharing your codes and for all you great work!
Hi,
I have made a small improvement to your code:
http://www.aeromental.net/2011/09/08/wordpress-how-to-create-thumbnails-in-your-index-from-youtube-videos-and-images-hosted-outside-imgur/
It can create thumbs for images hosted outside in another service like Imgur, and also creates a default image if nothing was found.
Thanks for the great work :) my blog looks better thanks to this.
You are welcome, thanks for sharing
Hey Vladimir thanks for this useful code but really i tired to get the images showing automatically in my posts in the home page i don't know what's wrong I've put the code in Function.php and again in home.php it still the same please help with these I'm using lifetime theme
Thank you so much for this function Vladimir! I love it and use it all the time. I had to make an adjustment recently to get it fetching the "featured image" first - posted the update on my (very lame) blog if you ever need it: http://blog.friendlywebconsulting.com/?p=177
Thanks again!
Michelle
I'm not really liking that it doesn't truly re-optimize the image to the correct size. It just changes the dimensions of the regular post image. This could be a page load nightmare. I'd say perhaps it's just not complete?
That's why this one is called 'simple' Jason :)
Jason, I think you can adjust that - just add this line in where you want to display the image:
$thumb=vp_get_thumb_url($post->post_content, 'thumbnail');
Change 'thumbnail' to whatever image size you'd like to display (thumbnail, medium, large or a custom size).
Cheers, Michelle
Great Job!
Really helpful.
Thanks a bunch for sharing!
:)
Hi there, great function. I have one question: when, in my post, I link to a photo from another website, the thumbnail pops up fine. However, when I use my own image, the thumbnail appears pixellated. I changed the width in the code that inserts the thumbnail on my index page (). What could be causing this?
Hi, had the same issue, have found the solution by any chance?
Hi Vladimir, great post,
was having a bitta trouble with your example code, kept getting "parse error" untill i realised your example code needed a ' (apostrophe) at the end... hope this helps anyone implementing the example code
Fixed, thanks for the notice.
Thanks Goran , had to struggle through it for a while before realising this post has not been updated with your code yet.
Great article. Thank!
Great Job!
Really helpful.
Thanks a bunch for sharing!
:)
How do I insert the above example usage code in the homepage loop? (I don't know php ;) )
Yes, you can supply a parameter to wp_get_attachment_image_src google the function
thank you very much! I think this is even better the the_post_thumbnail() function :)
is there a way to pull the medium sized image as opposed to the thumbnail? sorry, i'm not much of a programmer. thanks!
Thanks man! i was looking for somethign like this forever...
If video embedded in post, "preg_match" line should be changed to fetch ID:
preg_match("/([a-zA-Z0-9\-\_]+\.|)youtube\.com\/(v\/)([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $text, $matches2);
Thanks for the tip!
Thanks, this would remove my nightmare to manually assign thumbnail images :)