I am currently trying to output post thumbnail URL with its different image sizes; to use in ssrcset.

My current code is:

<img class="productimage lazyload" data-src="<?php the_post_thumbnail_url('medium'); ?>" />

But, this always outputs the full size image instead of 'medium'. Also, I have tried to use first string 'null' or post ID; but this still does not work.

Is there any way I can output post thumbnail URL with specfic image sizes I want; for srcset?

Well, it is a custom theme so I don't know if I missed something. Please let me know if there is a way to make it happen.

  • Is your original image smaller than the 'medium' image? If so then WP will return the 'full' image. Mar 26, 2021 at 11:04

1 Answer 1


Just use the the_post_thumbnail() function. It creates the image tag, adds srcset for you, and supports native lazy loading:

the_post_thumbnail( 'medium', [ 'class' => 'productimage' ] );

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.