Using Featured Images in Custom Post Types

If you purchase through a link on our site, we may earn a commission. Learn more.

Table of Contents

Custom Post Types are one of my favourite features in WordPress nowadays, and I use the excellent Easy Content Types plugin for generating them.

Sometimes I need to include uploadable images within the the custom post types, and although the plugin mentioned allows file uploads, I still feel that some tweaking of the ‘Featured Image’ functionality provided by WordPress is still the most ideal method of doing this.

So at the end of this tutorial we will have something that looks like the screen below

This is a screenshot of the ‘Books’ custom post type I have running on this website. The custom post type is used to display the Resources > Books part of the site. As you can see we have a custom meta box with some specific details about each book, as well as a place where to upload a screenshot of the book. This last item is using featured images.

I am not going to go into the creation of a custom post type and custom meta box as those are covered in our previous article about custom post types. By following that article you should be able to create the custom post type you need and also any custom meta boxes with custom fields if required.

Then to add the image upload we need to install the Multiple Post Thumbnails plugin. When that is installed you can open up your theme’s functions.php and add the following code:

[php]

$thumb3 = new MultiPostThumbnails(array(
‘label’ => ‘Book Screenshot’,
‘id’ => ‘book-screenshot’,
‘post_type’ => ‘book’
)
);
[/php]

This will create another featured image with name ‘Book Screenshot’ and associate with the ‘Book’ post type. And there you have it, that’s all you need from the admin side. On the front end, to display the image you can use the following code:

[php]
if (class_exists(‘MultiPostThumbnails’) && MultiPostThumbnails::has_post_thumbnail(‘book’, ‘book-screenshot’)) :
MultiPostThumbnails::the_post_thumbnail(‘book’, ‘book-screenshot’, NULL, ‘thumbnail’, array(‘class’=>’alignnone’) );
endif;
[/php]

If you enjoyed this post, make sure to subscribe to WPMayor’s RSS feed.

Jean Galea

Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Discover more from our archives ↓

Popular articles ↓

Share Your Thoughts

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

Claim Your Free Website Tip 👇

Leave your name, email and website URL below to receive one actionable improvement tip tailored for your website within the next 24 hours.

"They identified areas for improvement that we had not previously considered." - Elliot

By providing your information, you'll also be subscribing to our weekly newsletter packed with exclusive content and insights. You can unsubscribe at any time with just one click.