WordPress 2.9 – Using Post Thumbnails

With WordPress 2.9 comes a revised image thumbnail system – previously, support for posts with attached thumbnails was a little haphazard (it is a fiddly business in terms of the programming), but this is a significant step forward.

The need is for a series of images, of given size, associated with a series of posts – maybe for a front page with post excerpts and their images, or for a featured posts slideshow. Before 2.9, every theme developer had a different way of doing it – some with patent recipes for determining (guessing) which thumbnail was attached to which posts, while other themes required the web admin to add a thumbnail custom meta field manually, a bit tiresome.

add_theme_support()

Firstly, you do have to explicitly declare the new thumbnail system in functions.php – and best to wrap it up in an “if function exists” to avoid problems for older WP installs.

// to prevent  WP < 2.9 breaking
if (function_exists('add_theme_support')) {
add_theme_support( 'post-thumbnails' );
}

This produces a whole new little widget on admin post edit pages. In terms of admin, you can now attach a thumbnail image to a post using this widget, (or by adding an image via the media gallery, as before), and then taking the “Use as thumbnail” option

Usage

Now, you have the has_post_thumbnail() and the_post_thumbnail() template tags available – so in the post loop, say for home.php

<?php if ( has_post_thumbnail()) { ?>

	<div class="thumbnail">
		<a href="<?php the_permalink(); ?>">
		<?php the_post_thumbnail(); ?></a>
	</div>

 <?php	} ?>

has_post_thumbnail() tests whether a thumbnail exists for the post – if so, go on to display it with the_post_thumbnail(). You could also add an else here and go to display a default image if there isn’t a thumbnail

So far so good – but what size are the thumbnails going to be?

Sizing Thumbnails

This is where the complications arrive – but the good news there’s now various ways to control the whole system, giving you a better chance of getting what you want, fairly painlessly… There’s 3 main ways of doing it.

First, you can specify the image dimensions directly as variables in the template tag

	the_post_thumbnail(array(150,90));

Note that the variable is an array. This method is fine enough if the theme is simply using one size of thumbnail – it does give some odd results if you start elaborating, with different sizes on different pages, so…

Secondly, back in functions.php

if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails');
set_post_thumbnail_size(150, 90, true);
}

The set_post_thumbnail_size gives a default, here 150px width, 90px height – the 3rd variable is the “hard-crop” flag mode. 2 ways to go here, box-resizing (flag= false) and hard-cropping (flag=true).

Box resizing = shrinks an image until it fits the width or height value you’ve specified. So with this method, you always get the whole image in the thumbnails, but the thumbnails themselves might turn out to be different sizes, width or height.

Hard-cropping = the image is cropped to match the aspect ratio – width : height – and then shrunk to fit. So here you get all thumbnails the same size, but they may be missing cropped parts top/bottom or left/right. In practice, for general theme development, this is usually the best way to go.

Thirdly for sizing, you can use add_image_size()

if (function_exists('add_theme_support')) {
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 150, 90, true ); // default thumbnail size
	add_image_size('index-thumbnail', 120, 75); // for front page thumbnails
	add_image_size('single-post-thumbnail', 300, 180); // a different thumbnail size on single post pages
}

Now, you’ve done all the hard work and can be used very simply like this:

	<?php the_post_thumbnail('index-thumbnail'); ?>

Here, you have the most control over what’s going on.

I suspect there may be one or two refinements in the pipeline, but these methods have made it much easier to handle thumbnails in themes – not least you won’t have to show users how to add custom fields manually. Good effort….

Share:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • LinkedIn
  • Reddit
  • StumbleUpon
  • RSS
  • Twitter

7 Responses to “WordPress 2.9 – Using Post Thumbnails”

  1. Great info, will surely update some of our themes to use this thumbnail method :)

  2. Adam says:

    Is there any way to set custom thumbnail sizes for specific posts? For instance, I want the post thumbnails on my sticky posts to be much larger then the default size. Can’t find any information on this matter on the web.

  3. Walter says:

    My thumbnailsize is 186×80, and wordpress generates an 186×80 files, thus ok. But it also generates a 106×80 file and uses that as thumbnail in the index.php page. I can’t find anywhere in the files (in the template) where it gets this 106×80 ( I certainly don’t want it ), all is set to 186×80… drives me crazy.

  4. Daniel says:

    Really a terrific post. I googled a hack for TimThumbs and now it works just great with WP 3.01 – I find it totally kicks anything WP has built-in. You can specify image size, crop, quality, etc right in your URL!

  5. sudha Dubey says:

    This code help me alot.I have home.php and index.php the thumb nail image is appearing on index page i had put no code in index and as guided i had put it in home.php.But i am not getting the thumbnail image on home page.Also where to put this code:

Trackbacks

  1. WordPress UK » Blog Archive » WordPress 2.9 – Using Post Thumbnails
  2. WordPress 2.9 – Using Post Thumbnails | Themocracy WordPress Themes

Leave a Reply