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:

  1. 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.
  2. 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.
  3. Check if any images are found in the post and if yes, use the first one found as the thumbnail.
  4. 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 for functions.php

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 the_title(); ?>
< ?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 { ?>
Pembroke Athleta
< ?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:

  1. Get Parent Page Title and Permalink in WordPress
  2. How to Break Down a WordPress Post Title Into Two Parts
  3. Duplicating Pages/Posts Quickly in WordPress
  4. Using the NextGen Gallery for WordPress
  5. Using the Post Thumbnail Feature in WordPress

10 Comments

Got something to say? Feel free, I want to hear from you! Leave a Comment

  1. kevin says:

    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 ?

  2. Jean Galea says:

    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.

  3. kevin says:

    Ha ok, sorry for that. Anyway i found the solution ;)

  4. Ezequiel says:

    Hello mate , i put this in my wordpress but dont work :(

  5. Jean Galea says:

    Hey Ezequiel, did you include the timthumb.php script? Are you getting any errors?

  6. Skat says:

    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.

  7. Aakash says:

    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. :)

  8. alfiesaden says:

    hi there – is it just me !! can any one explain why when i type in the bing browser “www.jeangalea.com” i get a different site yet whe i type it in google its ok? could this be a bug in my system or is any one else having same probs ?
    sadensy

Trackbacks for this post

  1. WordPress Tutorials
  2. Busco este plugin!!!

Leave a Comment

Let us know your thoughts on this post but remember to place nicely folks!