Posted by & filed under WordPress.

As of WordPress 2.9 a very useful feature has been added that allows users to designate a thumbnail for each post, as well as its dimensions. This feature requires a slight modification to your theme to enable it, as well as some familiarity with some new related template tags.

As of WordPress 2.9 a very useful feature has been added that allows users to designate a thumbnail for each post, as well as its dimensions. This feature requires a slight modification to your theme to enable it, as well as some familiarity with some new related template tags.

Everything you need to know is written up in detail here. We’ve also written a function that will combine multiple techniques together in using the post thumbnail if it exists, or else defaulting to the first image found:

<?php
if (has_post_thumbnail()) {
	?>
	<a href = "<?php echo the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
	<?php
}
else {
	$attachments = get_children( array(
		'post_parent'    => get_the_ID(),
		'post_type'      => 'attachment',
		'numberposts'    => 1, // show all -1
		'post_status'    => 'inherit',
		'post_mime_type' => 'image',
		'order'          => 'ASC',
		'orderby'        => 'menu_order ASC'
		) );

	foreach ( $attachments as $attachment_id => $attachment ) {
		$firstimage = $attachment_id;
	}

	if (isset($firstimage)) {
		?>
		<a href = "<?php echo the_permalink(); ?>">
                <?php print wp_get_attachment_image($firstimage, array($width, $height)); ?>
                </a>
		<?php
	}
}
?>

Simply put this inside your WordPress loop and define $width and $height.