Search
Close this search box.

Using Featured Images in Custom Post Types

Table of Contents

Sponsored Ad

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

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 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.

Sponsored Ad

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

All suggestions are anonymous.

More from our blog...

Post a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Stay updated with WP Mayor's newsletter showcase every week

Stay on top of every new WordPress innovation and latest launches. Receive all our fresh product reviews and expert guides directly in your inbox.

Hosting Survey 2024

Are you happy with your hosting provider or are you over-paying for too little? Have your say below!

"*" indicates required fields

What's the main reason you picked this host?*
How happy are you with your host?*

OPTIONAL: If you'd like to receive the results of this survey, please enter your details below.