I have added an image size with code

add_image_size( 'medium_post_thumbnails', 87, 63, true);

By default WordPress has full/original image size.

I have a function where I use above image size 'medium_post_thumbnails', but I also have old posts where the image has original size.

echo '<div class="medium-news border-bottom margin-top">' .'<a href="'.get_permalink($post->ID). '">';
echo get_the_post_thumbnail($post->ID, 'medium_post_thumbnails', array('class' => 'img-fluid')). '</a></div>';

What I want to do here is if the 'medium_post_thumbnails' image size is not present, the function should use 'full' image.

  • There are plugins that will regenerate your images - meaning they go through all your media library, grab the originals, and generate sizes based on your current settings, so all of your images would then have your new medium_post_thumbnails size available. That might be more efficient than writing a code workaround. – WebElaine Apr 26 '18 at 18:13
  • @WebElaine Its not possible to do that there are 50,000 images. Thank you – Nimesh Apr 27 '18 at 1:25
  • 1
    Not sure why that wouldn't be possible, unless you're running out of disk space. – WebElaine Apr 27 '18 at 14:28

I don't see why something like this wouldnt work!

$med_post_thumb = get_the_post_thumbnail($post->ID, 'medium_post_thumbnails', array('class' => 'img-fluid')). '</a></div>';
$full_post_thumb = get_the_post_thumbnail($post->ID, 'FULL SIZE GOES HERE', array('class' => 'img-fluid')). '</a></div>';

if ($med_post_thumb == NULL) 
     echo $full_post_thumb;
     echo $med_post_thumb;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.