'WordPress Multiple Post Thumbnails Plugin – If 2nd Image Doesn't Exist, Then Default to Original Thumbnail
There is a slider on the homepage of a WordPress website I am working on. Because of the wide nature of the slider images, the automatic crop that Wordpress does when importing an image often chops off important parts of the pictures. For other purposes on the website, the normal thumbnail cropping isn't a problem. Just for the slider do I have an issue.
What I did is installed the "Multiple Post Thumbnails" plugin. This allows me to upload a second image that I pre-crop in Photoshop that is just for the slider on the home page. I can then upload a the un-cropped photo as the "Featured Image" that the site uses for everything else.
What I would like to set up is the ability for the slider to check for the "Multiple Post Thumbnails" first, if there isn't an uploaded photo, then to fall back on the regular "Featured Image."
I had assumed the code would look something like this:
<?php if(MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'featured-slider-image', NULL, 'main-slider') != NULL) : ?>
<?php MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'featured-slider-image', NULL, 'main-slider');?>
<?php else: ?>
<?php the_post_thumbnail('main-slider', array('alt' => esc_attr(get_the_title()), 'title' => '')); ?>
<?php endif; ?>
The logic I am going for is:
if: The Multiple Post Thumbnail Exists
then: Show the Multiple Post Thumbnail Image
else: Show the Featured Image Thumbnail
I have so far been very unsuccessful in trying to get that to work. Thank you for any help!
Solution 1:[1]
This is my solution and with me works good.
$imgURL = MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'grid-preview', NULL, 'large');
if ($imgURL){
MultiPostThumbnails::the_post_thumbnail( get_post_type(), 'grid-preview');
} else{
the_post_thumbnail('medium');
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alessandro Riccardi |
