How to Add Multiple Post Thumbnails / Featured Images in WordPress

How to Add Multiple Post Thumbnails / Featured Images in WordPress
Recently one of our readers asked if there was a way to add multiple post thumbnails or featured images in a WordPress post. By default, most WordPress themes come with built-in support for featured images which makes adding post thumbnails really simple. However when working on custom projects, you might need to add a second featured image to a post. In this article we will show you how to add multiple post thumbnails / featured images in WordPress.
First thing you need to do is install and activate the Multiple Post Thumbnails plugin. After activating the plugin, you need to add the following code in your theme’s functions.php file. You can add it anywhere in the file as long as you are doing it right. Read our guide on how to paste snippets from the web.
1if (class_exists('MultiPostThumbnails')) {
2 
3new MultiPostThumbnails(array(
4'label' => 'Secondary Image',
5'id' => 'secondary-image',
6'post_type' => 'post'
7 ) );
8 
9 }
Once you add that, you can now add a secondary featured image to your post. All you have to do is Edit an existing post or create a new post. You should see a secondary featured image meta box right after the featured image box in WordPress post edit area.
Featured and secondary featured image in post edit area
Even though you can add a secondary featured image in your WordPress admin, it will not display on your site until you configure your theme. All you need to do is add the following code inside your post loop where you want to display it. This could be in your single.php (for single posts), archive.php (only for archive pages), etc.
1<?php
2 
3if (class_exists('MultiPostThumbnails')) :
4 
5MultiPostThumbnails::the_post_thumbnail(get_post_type(),'secondary-image');
6 
7endif;
8 
9 ?>
Once you do that, you are done. Your theme is now ready to display multiple featured images. This is how it looked like when we tested it:
Adding Multiple Post Thumbnails or Featured Images in WordPress
You can repeat this process to add as many featured images as needed. You can also extend the support to other custom post types instead of just posts.

Adding New Thumbnail Sizes

If for some reason you need to change the thumbnail size for the secondary featured image, then you can do so by creating additional image size in WordPress. Don’t forget to regenerate thumbnails or new image sizes. After this you can call the new image size in your multiple post thumbnails code. For example, if you created a new image size with the name secondary-featured-thumbnail you would add this code in your template:
1<?php
2 
3if (class_exists('MultiPostThumbnails')) :
4 
5MultiPostThumbnails::the_post_thumbnail(get_post_type(),'secondary-image', NULL,  'secondary-featured-thumbnail');
6 
7endif;
8 
9?>
We hope that this article helped you add multiple featured images to your WordPress themes. What are some use cases that you can think of for this plugin? When can you see yourself adding multiple post thumbnails in WordPress? Let us know by leaving a comment below.

0 comments: