get_the_post_thumbnail_url with an unregistered size

WordPress does not create any thumbnail on the fly. If you try to fetch a size that doesn’t exist, either the closest size or the full size image will be retrieved.

The closest solution you can try is to fetch a size larger than what you want, and then size it down via CSS and crop it.

Let’s say the registered sizes are 50 x 150, 300 x 300 and 1024 x 1024, but you need a 300x177 image. Fetch the 300x300 one, and then clip it using CSS:

#my-div img {
    position: absolute;
    clip: rect( 0px, 300px, 177px, 0px );
}

Leave a Comment