Post thumbnails and default images

In my previous post about post thumbnails with posts excerpts I shared code to display the first image of your post as a thumbnail in your main page and it displayed a default image if the post didn’t have any images in it.

Now that WordPress 2.9 was released we can use new functions to display the post image as a thumbnail. This feature is only available if it is enabled specifically in the theme, so to enable it all you have to do is add this 2 lines to the functions.php file of your theme.

if ( function_exists(‘add_theme_support’) )
add_theme_support(‘post-thumbnails’);

This will enable the post thumbnail functionality and add a new box in your new post pages to set the post thumbnail

post_thumbnail_box

post_thumbnail_box2

To set an image as thumbnail you upload it like any other image or select it from your media library and at the bottom you will see the text “Use as thumbnail”, click on it and that’s it.

post_thumbnail_3

Now, to display the thumbnail in your theme you have to use the function the_post_thumbnail(),  the basic usage should be :


if ( current_theme_supports( 'post-thumbnails' ) )
the_post_thumbnail(array(100,100));

Which is pretty simple, and integrated  into the code we have from the previous post about thumbnails should look like this:


if ( function_exists('has_post_thumbnail') && has_post_thumbnail() ) {
the_post_thumbnail(array(100,100));
} else {
postimage(100,75,'image for: '.get_the_title(),'image');
}

Comments are closed.