Post Thumbnail Images new feature of wordpress 2.9

Posted on January 29th, 2011

With WordPress 2.9, theme authors can easily enable Post Thumbnail selection UI and call those image using simple template tags.

First, in the theme’s functions.php, declare that your theme supports this feature. This will enable the UI in the WP Admin.

add_theme_support( 'post-thumbnails');

Next, you should specify the dimensions of your post thumbnails. You have two options here: box-resizing and hard-cropping. Box resizing shrinks an image proportionally (that is, without distorting it), until it fits inside the “box” you’ve specified with your width and height parameters. For example, a 100×50 image in a 50×50 box would be resized to 50×25. The benefit here is that the entire image shows. The downside is that the image produced isn’t always the same size. Sometimes it will be width-limited, and sometimes it will be height-limited. If you’d like to limit images to a certain width, but don’t care how tall they are, you can specify your width and then specify a height of 9999 or something ridiculously large that will never be hit.

set_post_thumbnail_size( 50, 50 ); // 50 pixels wide by 50 pixels tall, box resize mode

Now, you can make use of the template functions to display these images in the theme. These functions should be used in the loop.

has_post_thumbnail() returns true/false and indicates whether the current post has a manually-chosen Post Thumbnail (in the loop):

the_post_thumbnail() outputs the Post Thumbnail, if it exists (in the loop):

What if you want to use a small 50×50 hard-cropped image for the home page, but want to use a 150 pixel-wide and 150 height image on the single post page. You can specify additional custom sizes! Here’s the code:

add_image_size( 'single-post-thumbnail', 150, 150 );

then call the Function

<?php the_post_thumbnail( 'single-post-thumbnail'); ?>