Multiple post thumbnail (featured image) sizes in WordPress

I’ve needed to, on multiple occasions, output post thumbnails for different content types at different sizes.

This is the code you probably have:

add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 94, 144, true );

At this point, all instances of the_post_thumbnail will output the image at 94×144. There are two main ways to output a different size: using predefined image sizes, or by specifying your own. Using the_post_thumbnail('thumbnail'), for instance, will display the thumbnail using your install’s thumbnail settings, in Settings > Media. This too, however, can only be set globally, and isn’t ideal since the thumbnail size is likely used elsewhere on your site.

You can also output a thumbnail using an array – the_post_thumbnail( array( 100,150) ). This is less than ideal as well because the resizing is done on the fly – either by specifying a size in the HTML or by regenerating it server-side, but either way is unnecessary load on the server.

The best way to approach the problem to create new custom image sizes, essentially in addition to the default ‘thumbnail’, ‘medium’, and ‘large’.

add_image_size( 'author-thumbnail', 60, 60, true );

This will add the extra work to the media manager – when you upload an image, it will resize one additional time to these specifications and that new image will be at the ready using its name, in this case ‘author-thumbnail’. Now, we can simply do this in our template file to output the properly sized thumbnail for the content type:

the_post_thumbnail( 'author-thumbnail' )

2 Comments

  1. Andresa
    Jan 26, 2011 @ 13:39:01

    This post is exactly what I was looking for. Thank you!

    Reply

  2. keitai
    Feb 26, 2011 @ 12:41:58

    Hi Gavin,

    Your suggestion is almost what i want. In my case i have set a custom post type called Reviews which i want to have a different sized feature image. Now by using your suggestion it works on the front end, but in admin i like to see the altered sized thumbnail as well.
    Secondly, i guess every image has a resized image, but i like to have it only resized when the image belongs to the custom post type.

    Something like this maybe??
    function alter_image(){
    global $post;
    if($post->post_type == ‘Reviews’) {
    add_image_size( ‘review-thumbnail’, 150, 200, true );
    }
    }

    regards,

    Reply

Leave a Reply