WordPress Tutorial: Custom Thumbnail Sizes

WordPress offers a decent media management solution featuring various media types such as images, PDFs, etc. By default, post thumbnails are inactive in WordPress and will need to be switched on to allow editors to use the 'Featured Image' image manager when creating and editing posts.

Featured images are great for post teasers where a small amount of textual content is shown alongside an image. Another plus for WordPress is the ability to specify custom image sizes for the Featured Image thumbnail. This allows you to alter the size of thumbnails for different parts of the site or maybe you have a custom post type that requires its own image size. Whatever the case, using the following code will allow you to define and display custom post thumbnails.

if (function_exists('add_theme_support')) {
    add_theme_support('post-thumbnails');
    set_post_thumbnail_size(160, 160);
    add_image_size('teaser-thumbnail', 110, 110, true);
    add_image_size('home-thumbnail', 126, 126, true);
    add_image_size('single-view-thumbnail', 57, 57, true);
}

To display the custom thumbnail you would then use the following snippet within your theme:

get_the_post_thumbnail($post->ID, 'teaser-thumbnail');

One thing to note about this technique is that custom thumbnail sizes only take effect from the moment you activate them, old image uploads will not have a custom size assigned to them. To get all of your old images resized to your custom settings you can use a plugin to regenerate your thumbnails: http://wordpress.org/extend/plugins/regenerate-thumbnails/