How to Add Featured Image Thumbnails to Your WordPress Theme

Share this article

You may have noticed the “Featured Image” box when editing a post or page. It allows you to upload or select an associated image for the article. It generally appears as a thumbnail when viewing a list of posts, perhaps in a category index or search results. Thumbnail support must be enabled within your theme. You could add it to a plug-in
so it’s available to all themes but that may not be appropriate in every case. Therefore, you’ll need to open or create a ‘functions.php’ file within your theme folder (wp-content/themes/theme-name/). To add thumbnail support for all post types, append this command somewhere after the opening <php:

add_theme_support('post-thumbnails');
Alternatively, you can enable thumbnails for just posts:

add_theme_support('post-thumbnails', array('post'));
or just pages:

add_theme_support('post-thumbnails', array('page'));

Setting Thumbnail Sizes

Default thumbnail sizes can be set in WordPress’s Settings > Media screen. However, you can also set a default height and width in functions.php, e.g.

set_post_thumbnail_size(100, 75);
This will produce thumbnails with 100px width and a 75px height (a pleasing 4:3 ratio). However, what happens when a user uploads an image with different aspect ratio — say 100 x 150? In this case, the whole image is proportionally reduced to fit the space and the resulting thumbnail is 50 x 75. Alternatively, you can define hard-cropping by passing ‘true’ as a third argument:

set_post_thumbnail_size(100, 75, true);
This will crop the image so it matches the aspect ratio. The resulting thumbnail will always be 100 x 75, but the top and bottom or left and right edges will be removed. WordPress Featured Image The “Featured Image” box should now appear on your WordPress post/page edit screen. If it’s not there, check it’s enabled in “Screen Options” or review your functions.php code.

Using Thumbnails

Three main thumbnail commands can now be used within any WordPress loop. Typically, you’ll want to use them in files named index.php, category.php, archive.php, author.php, taxonomy.php or search.php:
  • has_post_thumbnail() returns ‘true’ if a thumbnail has been set
  • the_post_thumbnail() echos a string containing the thumbnail <img> tag
  • get_the_post_thumbnail() returns a string containing the thumbnail <img> tag
One of the simplest implementations is therefore:

if (has_post_thumbnail()) {
	the_post_thumbnail();
}
Or we could add a link and a default thumbnail if one isn’t available:

echo '<a href="', get_permalink(), '">';
if (has_post_thumbnail()) {
	the_post_thumbnail();
}
else {
	echo 
		'<img src="',
		get_bloginfo('template_directory'), '/images/thumb-default.png',
		'" width="100" height="75" alt="thumbnail" />';
}
echo '</a>';

Advanced Thumbnail Use

Two optional arguments can be passed to the_post_thumbnail() and get_the_post_thumbnail(). The first is the size — either:
  1. a string containing the text ‘thumbnail’, ‘medium’ or ‘large’ as set in in WordPress’s Settings > Media screen, or
  2. an array with new width and height dimensions, e.g. array(120, 90)
The second is an associative array containing the src, class, alt and title. For example:

the_post_thumbnail(
	array(120, 90), 
	array(
		'src' => 'image.jpg',
		'class' => 'thumbnail',
		'alt' => 'post thumbnail',
		'title' => 'my custom title'
	)
);
results in HTML such as:

<img width="120" height="90" src="image.jpg" alt="post thumbnail" title="my custom title" />
That’s about as complex as it gets. Have fun adding thumbnail support to all your WordPress themes. If you enjoyed reading this post, you’ll love Learnable; the place to learn fresh skills and techniques from the masters. Members get instant access to all of SitePoint’s ebooks and interactive online courses, like The Beginner’s Guide to Web Design with WordPress.

How Can I Add Multiple Featured Images in WordPress?

WordPress by default allows you to add only one featured image or post thumbnail. However, you can add multiple featured images by using a plugin like Dynamic Featured Image. After installing and activating the plugin, you can add multiple featured images to your posts from the post edit screen. Remember to update your theme files to display these images correctly.

Why Isn’t My Featured Image Showing Up in WordPress?

There could be several reasons why your featured image isn’t showing up. It could be due to a theme or plugin conflict, incorrect image size, or your theme might not support featured images. Try deactivating your plugins one by one to see if any of them are causing the issue. If that doesn’t work, check your theme’s functions.php file to ensure it supports post thumbnails.

How Can I Change the Size of My Featured Images?

You can change the size of your featured images by adding a custom function in your theme’s functions.php file. Use the add_image_size() function to define a new image size. Then, use the_post_thumbnail() function in your theme files to display the new image size.

Can I Add Featured Images to My Old Posts?

Yes, you can add featured images to your old posts. Simply edit the post and set the featured image from the post edit screen. If you have a large number of posts, you can use a plugin like Quick Featured Images to bulk set featured images.

How Can I Add a Default Featured Image in WordPress?

You can add a default featured image by using a plugin like Default Featured Image. After installing and activating the plugin, go to Settings > Media to set your default featured image. This image will be used whenever a post does not have a featured image set.

How Can I Display Featured Images in RSS Feeds?

By default, WordPress does not include featured images in RSS feeds. However, you can add them by using a plugin like Featured Images in RSS & Mailchimp Email. Alternatively, you can add a custom function in your theme’s functions.php file to include featured images in your RSS feeds.

Can I Add Featured Images to My Categories and Tags?

Yes, you can add featured images to your categories and tags by using a plugin like Categories Images. After installing and activating the plugin, you can set featured images for your categories and tags from the edit screen.

How Can I Add a Featured Image Column in the WordPress Admin?

You can add a featured image column in the WordPress admin by using a plugin like Featured Image Admin Thumb. This plugin adds a new column in the post list to display the featured image of each post.

How Can I Add a Hover Effect to My Featured Images?

You can add a hover effect to your featured images by using CSS. Add a hover selector to your theme’s style.css file and define the desired effect. For example, you can change the opacity of the image on hover to create a fade effect.

Can I Add a Lightbox Effect to My Featured Images?

Yes, you can add a lightbox effect to your featured images by using a plugin like Simple Lightbox. After installing and activating the plugin, your featured images will open in a lightbox when clicked.

Craig BucklerCraig Buckler
View Author

Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's created enterprise specifications, websites and online applications for companies and organisations including the UK Parliament, the European Parliament, the Department of Energy & Climate Change, Microsoft, and more. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler.

featured imagethumbnailsWordPress
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form