Jean is an Internet and Multimedia specialist with a keen interest in music, technology, business, sports and psychology.
Jean Galea
Internet Consultant in Malta
Jean Galea: entrepreneur and internet specialist. Holding an M.Sc. in Multimedia & Internet Computing from Loughborough University, Jean specialises in web design and development, email marketing systems and general internet consultancy.
The Ultimate WordPress Thumbnail Hack
Ok, so there are a number of hacks out there that help you fetch a thumbnail to go with your post summary, via different methods. Here is one hack I created that uses all the best sources I’ve found fused into one.
Our final script will follow the following process in this order:
- Check if Post Thumbnail has been set (a specific feature for setting thumbnails only available in WordPress 2.9 and above). If it is set use that as the post thumbnail.
- Check if a custom field has been set with the key ‘thumbnail’ and the URL of the image as its value. If it is set use that for a thumbnail.
- Check if any images are found in the post and if yes, use the first one found as the thumbnail.
- If none of the above conditions were satisfied, display a default thumbnail.
The script also provides for resizing of images via the timthumb.php script (see instructions for downloading that below).
NOTE: I am having problems displaying code here on the blog post using WP Syntax, so until I sort that out it is better to download the code from these links:
Code to go in your template file (probably index.php)
So, let’s get cracking. First off you have to open your functions.php file (found with the rest of your template files) and insert the following code:
< ?php
// see if there is any image in the posts_nav_link
function findImage() {
$content = get_the_content();
$count = substr_count($content, '
0) return true;
else return false;
}
// retreives image from the post
function getImage($num) {
global $more;
$more = 1;
$content = get_the_content();
$count = substr_count($content, '
');
$postOutput = substr($post, 0, $imgEnd+1);
$image[$i] = $postOutput;
$start=$imgEnd+1;
$cleanF = strpos($image[$num],'src="')+5;
$cleanB = strpos($image[$num],'"',$cleanF)-$cleanF;
$imgThumb = substr($image[$num],$cleanF,$cleanB);
}
if(stristr($image[$num],'![]()
Then in your template file you need to insert this other code:
< ?php
$postimageurl = get_post_meta($post->ID, 'thumbnail', true);
/* Check if Post Thumbnail has been set (WordPress 2.9 and above) */
if ( (function_exists('has_post_image')) && (has_post_image()) )
{ ?>
< ?php echo (the_post_image('thumbnail')); ?>
< ?php }
/* Check if custom field URL is set */
else if ($postimageurl) { ?>
< ?php }
/* Check if any images found in post and display the first one found */
else if ((function_exists('getImage')) && (function_exists('getImage')) && (findImage())) { ?>
< ?php }
/* Display default image */
else { ?>
< ?php } ?>
You can set the size of the images in the script (I am using 115px by 115px).
For this to work you also need to download the timthumb.php script from here
Just put the timthumb.php file together with your theme’s files.
If you have any advice for optimising this code do let me know, as I am by no means an expert PHP coder.
Related posts:
- Using the Post Thumbnail feature in WordPress
- Using the NextGen Gallery for WordPress
- Rotating Images in WordPress
- PHP Notes – POST Vs. GET
- Use WordPress functions outside of WordPress
9 Responses to “The Ultimate WordPress Thumbnail Hack”
Leave a Reply
Thank you for reading this blog. Please check back for new posts.


Hey jean, just got answer to my message on problogdesign.com. I dont see any function or code here to retrieve the height of the picture taken by the srcipt. Is there any ?
Hey Kevin, thought I posted a general comment to the original article on problogdesign, apologies if I replied directly to your message instead. My code here doesn’t retrieve the height as you wanted, will post back if I come across something that does do that.
The Ultimate Thumbnail Hac…
Ok, so there are a number of hacks out there that help you fetch a thumbnail to go with your post summary, via different methods. Here is one hack I created that uses all the best sources I’ve found fused into one
……
Ha ok, sorry for that. Anyway i found the solution
Hello mate , i put this in my wordpress but dont work
Hey Ezequiel, did you include the timthumb.php script? Are you getting any errors?
Magnifique. Just what i was looking for. All in one: wp thumbs, custom fields thumbs, timthumb resized images and default image if none. The script works like charm. Thanks a million.
[...] Without Custom Field | Pro Blog Design …….. y que aca habia una forma mas completa del mismo The Ultimate WordPress Thumbnail Hack – Jean Galea – Internet Consultant in Malta | Word… ……… ya los revise pero tengo muchas dudas…. pues apenas comienzo con wordpress….. alguien [...]
Your post is a life saver! It is absolutely the best hack for wordpress thumbnails. The thumbnail issue had been troubling me for the past few days. After experimenting with a number of hacks, I had to settle with the sixth one listed here
http://malarvizhik.wordpress.com/2010/05/13/10-exceptional-wordpress-hacks/
Your solution is though one step ahead of that, I just don’t have the energy to implement it. Nevermind, I’m going to utilize it on my next blog. Thanks a bunch.