Display Thumbnails in WordPress Posts using the “Timthumb Script”


Timthumb is a script by Darren Hoyt that can scale images to any size. In this case, we will use it to refer to an image and display it as a post thumbnail.

As prior to version 2.9 there was no predefined way to add a post thumbnail, we’ll have to add it as custom information. The easiest way to do this is by adding a custom field.

Custom fields can be added in the editor, a bit under the text area like in the image below:

In this case, we will use the name “Image” (you can give it any name) and for the value the image’s URL (relative URLs are also supported). By default only images from the current domain are displayed. To allow images from different domains, these have to be added as exceptions. This can be done by directly editing the timthumb script and adding a value to the variable $allowedSites. Just search for it, it’s somewhere at the beginning.

Now that we have out thumbnail declared, we must display it. For this add the following function:

<?php function my_post_thumbnail($width,$height) {

  $thumb = get_post_custom_values("Image");
  if (!empty($thumb)) : ?>
    <img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo $thumb[0]; ?>&w=<?php echo $width; ?>&h=<?php echo $height; ?>&zc=1&q=100" alt="<?php the_title(); ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" border="0" />
  <?php endif; ?>
  return !empty($thumb);
Then call the function at the location where you want to display the thumbnail:
<?php my_post_thumbnail(300,300); ?>

Of course you can call directly the code in the function, I just find it more elegant this way. Now let’s see how we can display a thumbnail if the user hasn’t selected any. The returned value can be used to put the function in an if statement.

If this post helps you any way plz Join techinfoBiT on facebooktwitter,  or subscribe blog-feed.


  1. Nice read, I just passed this onto a colleague who was doing some research on that. And he just bought me lunch as I found it for him smile So let me rephrase that: Thanks for lunch!


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.