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
    Commented Apr 26, 2018 at 18:13
  • @WebElaine Its not possible to do that there are 50,000 images. Thank you
    – Nimesh
    Commented Apr 27, 2018 at 1:25
  • 1
    Not sure why that wouldn't be possible, unless you're running out of disk space.
    – WebElaine
    Commented Apr 27, 2018 at 14:28

1 Answer 1


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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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