News

Using TimThumb with WordPress Post Thumbnails

The WordPress post thumbnails functionality has become a basic necessity of theme coding, but it does have its limitations. Mainly you are stuck with the image sizes you specify in the Settings – Media tab. You can also specify your own image sizes, but clients undoubtedly want to change their image sizes over time or within one website over and over and over and over…. So it makes sense to make use of the dynamic image resizing script TimThumb. I use the following technique to grab the designated featured image post thumbnail from a post and then automatically resize it upon display. Works perfectly!

                        
<?php
// check if the post has a featured image assigned to it.
if ( has_post_thumbnail() ) {
// get the src of the large size featured image
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );
$thumbnailSrc = $src[0];
// output image resized with timthumb 
?>
<a href="<?php the_permalink(); ?>">
<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $thumbnailSrc; ?>&h=420&w=482" alt="">
</a>
<?php } ?>

4 Responses to “Using TimThumb with WordPress Post Thumbnails”

  1. Chris says:

    Thanks for the code snippet. It works like a charm.

  2. Martin says:

    Thanks, exactly what I was looking for! :D

  3. cn arabia says:

    I am trying to use this in my site but it display images upload in directory wp-contents/uploads , most of the images in my posts are from external site or flickr . It is not displaying thumnail from those site. Is there any thing to make generation of thumbnail from them possible ?

  4. Laura McDonald says:

    I believe your problem, cn arabia, is from the security settings with the TimThumb config. You have to set it to allow external sites, by default it doesn’t allow them. So in your config file (if you have it) or in thimthumb.php, look for the area that says “external domains that are allowed to be displayed on your website” and below that add in whatever websites you want to allow external images to be fetched from.

Leave a Reply

Preserve code formatting within <code> and <pre> tags

What is 8 + 8 ?
Please leave these two fields as-is:
Please help us combat comment spam by answering this question.