WordPress: Feature Image and Post Thumbnails

In WordPress there’s a feature image box in post edit screen. However this feature is theme dependent and is called post thumbnails. It’s a new feature introduced in V2.9 and is enabled through add_theme_support().

This post demonstrates the most basic way to enable and display a feature image.

Add the following code to functions.php:

add_theme_support( 'post-thumbnails' );

Feature image meta box will show up on the right side of post edit screen. Simply click ‘set feature image’ and follow instruction to add / select a feature image.

To show the feature image in your theme, add the code to the template (within the loop):

if (has_post_thumbnail())
    the_post_thumbnail();

the_post_thumbnail() directly displays a preformatted <img> code.

For more control of HTML code, call wp_get_attachment_src(get_post_thumbnail_id()) to get the source of file and do your own formatting.

Leave a Reply

Your email address will not be published. Required fields are marked *