Published

Thumbnails are really handy, depending on the type of site you're designing. The nice thing is that WordPress automatically generates several thumbnail sizes of uploaded images.

You likely noticed that the title uses the term Snippets as opposed to Plugins. These could be put into plugin form, but they can also be added to a WordPress theme's functions.php file. I'm not sure of the version compatibility of the code; it's been tested with WordPress 2.7.

Post Thumbnail

This is a minimal-overhead thumbnail solution. It makes use of WordPress's default thumbnail behaviour. I like this approach because it's relatively foolproof if I'm asking non-technical clients to upload photos—they don't have to know anything about the thumbnails, they just need to dump in the source file.

If you look under Settings > Media in the WordPress admin, you'll see three image sizes that you can configure. When you add an image to the media library of a post, WordPress will upload the original and save three smaller versions (provided that the image you uploaded was bigger than these in the first place). So in the end you'll have four image sizes: full (original), large, medium, and thumbnail.

If you plan your design carefully, this should be enough sizes to make you're life pretty easy. For instance, I designed a site where various products were stored as posts. For each post, I added a product photo to the media library. I had 150 x 150 thumbnails in the catalogue, 200 x 133 thumbnails on the home page, and a 300 x 200 size on the single product page. The user could then click to view the full size.

I made all of this happen by adding the following code to the theme's functions.php:

function get_image_url($parent_id, $size='thumbnail', $return=false) {
    $images =& get_children('post_parent=' . $parent_id .'&post_type=attachment&post_mime_type=image');
    if (is_array($images)) {
        foreach ($images as $attachment_id => $attachment) {
            $result = wp_get_attachment_image_src($attachment_id, $size);
            $result = $result[0];
            if ($return)
                return $result;
            else {
                echo $result;
                return;
            }
        }
    }
    else
        if ($return)
            return false;
}

Then, when I want the URL to the medium thumbnail for a post, I just use this in my template:

get_image_url($post->ID, 'medium');

Essentially, this will grab the first image in the media library of the given post.  Like I said, the size options are 'full', 'large', 'medium', and 'thumbnail'. The function echoes the URL by default.  If you want to store the URL for use later, set the $return parameter to true.

Category Thumbnail

In the same vein, I've needed a category thumbnail on a couple of occasions, when most of my posts are prominently associated with images.

Please note that the following snippet makes use of a modified version of Mike Stickel's Random posts from random terms plugin. I just modified it for easier access to his useful utility functions.

The function grabs a random post from a specific category and gets its thumbnail (using the previous function):

function get_category_image_url($category_id, $size='thumbnail', $return=false) {
    $random_post = ms_random_post($category_id, 1, false);
    if (sizeof($random_post) > 0) {
        if ($return)
            return get_image_url($random_post[0]['id'], $size, true);
        else
            get_image_url($random_post[0]['id'], $size, false);
    }
    else
        if ($return)
            return false;
}

Now I can quickly get a relevant thumbnail for a category:

get_category_image_url($category_id);

As you can see from the function definitions, size defaults to thumbnail.

If these snippets don't do exactly what you need them to do, they are hopefully simple enough that you can adapt them to your requirements.

Alternatively...