0

I know that Wordpress has its way of letting the browser decide which image to download depending on the viewport via the srcset.

I also know that I can use the_post_thumbnail function and specify a custom size, like this:

the_post_thumbnail('my-custom-size');

Now, what I would like to achieve is being able to specify a list of custom sizes (of different aspect ratio from the original image), and letting the browser decide with one to use based on the viewport.

I DON'T want to just add a custom size to the list of possibilites in srcset, I want to choose the whole list of available sizes.

I DON'T want to override the general setting of srcset of the entire website. I want to specify this list of sizes only in one section.

Is this possible?

1 Answer 1

1

The 2nd argument of the_post_thumbnail() is an array of attributes and values, so you can just set your own srcset attribute that way:

the_post_thumbnail( 
    'my-custom-size', 
    array(
        'srcset' => wp_get_attachment_image_url( get_post_thumbnail_id(), 'my-other-custom-size' ) . ' 1000w',
    )
);
2
  • 1
    +1 get_post_thumbnail_id is the correct function Commented Sep 8, 2021 at 19:45
  • Hey buddy, I know this isn't the right place, yet I had a question very similar to this, how am I supposed to use wordpress's built in image sizes and club them all together in SRCSET... wordpress.stackexchange.com/questions/395376/… Please Help Commented Sep 9, 2021 at 3:10

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.