Wordpress 2.9 the_post_thumbnail

Fresh off the presses

The release of Wordpress 2.9 has been very exciting and we’re glad to see some solid core enhancements being rolled out. Among the most popular additions is the ability to directly associate thumbnails with a post without having to mess around with custom fields. Some downsides include the lack of support for posts without images, and full control over the outputted IMG tag. Of course you can always still use custom fields, so no functionality has been lost. In this short post we’ll go over how to add thumbnails to posts.

Adding support

Although 2.9 supports post thumbnails, this functionality isn’t active yet, so we’ll need to add support in manually. Don’t freak it’s just a single line of code. Open up your functions.php in your theme’s directory, and add the following line:

add_theme_support( 'post-thumbnails' );

Attach a thumbnail in your post

There should now be a thumbnail modal box in the lower right hand corner of your screen when editing a post. The interface is extremely intuitive if you’ve ever attached an image before so it’s primarily self-explanatory. You might miss the “use as thumbnail” link, so here’s a screen of where to find it:

Showing the thumbnail

Now it’s time to show the thumbnail in your theme. There are many configuration options to play with, but the default settings will do just fine since you can always take care of sizing with CSS. A simple template tag snippet calls your image:

the_post_thumbnail();

And that concludes this crash-course to using the new thumbnail functionality. Head on over to the Wordpress official site for more in depth documentation of all the neat bells and whistles.

WPRecipes has a nice snippet that integrates legacy support as well:

http://www.wprecipes.com/wordpress-2-9-display-post-image-with-backward-compatibility

<?php
	if (  (function_exists('has_post_image')) && (has_post_image())  ) {
		the_post_image('thumbnail');
	} else {
		$postimageurl = get_post_meta($post->ID, 'post-image', true);
		if ($postimageurl) {
			echo '<img src="'.$postimageurl.'" alt="" />';
		}
	}
?>

Powered by Olark