Use a thumbnail (or feature image) for your post automatically

by Moises Kirsch

Instead of creating thumbnails or special images for each post, I found that it is better to upload a full size image and then have it resized on the server on the fly (with some type of cache mechanism of course).

For this, I usually use a script such as Smart Image Resizer working together with the following code in my funtions.php file:

function get_first_image($id){
	$attachments = get_children( array(
		'post_parent' => $id,
		'post_status' => 'inherit',
		'post_type' => 'attachment',
		'post_mime_type' => 'image',
		'order' => 'ASC',
		'orderby' => 'menu_order ID'
	));

	// If the post has no image return a default
	if(empty($attachments)){
		return get_bloginfo('template_directory').'/images/default.jpg';
	}
	else{
		$attachment = current($attachments);
		return $attachment->guid;
	}
}

Then you use the Smart Image Resizer inside the loop like this:

<img src="<?php echo get_bloginfo('template_directory').'/images/image.php?width=200&amp;height=200&amp;cropratio=1:1&amp;image='.get_first_image($post->ID); ?>" alt="The Great Image" />

This example would display a 200 pixels squared thumbnail of the first image attached to the post.

With this technique you could display your posts in tons of different way… you can mix it up with some CSS for backgrounds, with some javascript to create a slider or a carousel, etc.