
How to Add Multiple Featured Images for a Custom Post Type
- September 23, 2016
- Leave a comment
WordPress supports only one featured image by default for a custom post type or default post. But you can extend it to display multiple featured images as per your requirement.
Sometimes, you may want to display more than one featured image. Let’s assume you want to display a slideshow of a specific car (here, car is a custom post type). Some libraries and plug-ins can do it for you; the most common among them is “Multiple Post Thumbnails”. You need to extract its files and then include them in your Theme.
After that, you need to upload some additional featured images and associate them with the custom post type by using the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if(class_exists('MultiPostThumbnails')) { newMultiPostThumbnails(array( 'label'=>'2nd Feature Image', 'id'=>'feature-image-2', 'post_type'=>’cars’ ) ); newMultiPostThumbnails(array( 'label'=>'3rd Feature Image', 'id'=>'feature-image-3', 'post_type'=>’cars’ ) ); }; |
You have successfully added support for three featured images to your ‘cars’ custom post type. Use the following code to get the image source at the front-end:
1 2 3 4 5 |
$img_name ='feature-image-2'; //And so on $img_id= MultiPostThumbnails::get_post_thumbnail_id(‘cars’,$img_name,$post->ID ); // use the MultiPostThumbnails to get the image ID $img_src_url= wp_get_attachment_image_src($img_id,'feature-image'); // define full size src based on image ID |
After getting the image source for all the featured images, pass them onto a slider or carousel accordingly.
i use this code for multiple featured image for custom post type but its not working..
Hi Aditya,
Just to be clear have you installed “Multiple Post Thumbnails” plugin? if not then this code is not valid without that.
Make sure the plugin is updated.
Let us know if you need further assistance.
Regards,
—
Azeem | Client Service Executive
Email: support@presstigers.com
I use this code but it doesn’t work. I add multiple post thumbnail plugin also. But I am unable to display featured image on my custom post type page.