Snippet: Adding Post Thumbnail in WordPress 2.9

When surfing the Web, you can easily come across WordPress sites that associate an image thumbnail with each post. This is particularly obvious for those blog with magazine style layout. Before WordPress 2.9, the only way to do this is to insert a custom field with the URL of the image thumbnail in each post/page. Now this feature is built into the WordPress core and you can use it without using any custom field.

Firstly, in your theme folder, open the functions.php file and insert the following function to the end of the file.

if(function_exists('add_theme_support')){
    add_theme_support( 'post-thumbnails' );
}

Save and upload the file back to the server.

Now in your Post/Page edit section, you will find a Set thumbnail option in the rightmost column (just below the Categories section).

wp-thumbnail

Click on it. The image uploader window will appear. You can either new image (or select from Gallery) the picture to be used as the thumbnail. Once you have uploaded, click on the use as thumbnail option.

wp-set-new-thumbnail

Save the post.

Getting the thumbnail to appear in the front end

To get the thumbnail image to appear, you need to insert the function

if ( has_post_thumbnail() ) {
	    // the current post has a thumbnail
	} else {
	    // the current post lacks a thumbnail
	}

in your theme files. The most likely place is index.php, single.php and page.php

Check out Mark Jaquith’s article for more detail on the usage of the thumbnail function.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Damien Oh Avatar

Read next

Suzanne Simard sealed paper birch and Douglas fir seedlings inside plastic bags, fed them carbon-14 and carbon-13 dioxide, and nine days later found carbon had crossed between species through fungal threads in the British Columbia soil beneath her boots
A species of jellyfish called Turritopsis dohrnii can revert its adult cells back to a juvenile polyp stage when injured or starving, effectively restarting its life cycle, and biologists have so far failed to identify any natural limit to how many times it can do this.
A Japanese man named Jiroemon Kimura, who lived to 116, was born in 1897 when Queen Victoria still ruled and died in 2013, meaning a single human life personally overlapped with the invention of the airplane, the atomic bomb, the internet, and Instagram
The Hollywood sign originally read HOLLYWOODLAND when it was built in 1923 as a real estate advertisement for a housing development, and it was only meant to stand for 18 months, but nobody ever got around to taking it down and the city eventually adopted it as a landmark
Almost all of the world’s internet traffic does not travel by satellite but through fibre-optic cables lying on the ocean floor, a hidden web of wires crossing the deepest parts of the sea to connect the continents.
People who flip their phone face down on every table aren’t being secretive. They figured out that staying interruptible meant handing their time to whoever rang first
Twitch vs. Facebook Gaming vs. YouTube Gaming: What’s the Best Live Game Streaming Platform?
Chrome Extensions Ownership Transfer is a Direct Threat to You: How to Stay Safe