I am trying to get an image with it’s srcset attribute for all the image sizes using wp_get_attachment_image(). But it seems like the “full” (original) size is not listed:

<?php echo wp_get_attachment_image( $imageid, 'medium', false, $image_attributes); ?>

Gives: (I excluded non relevant attributes)

<img src="https://wordpress.stackexchange.com/questions/267550/...image-232x300.jpg"
    srcset="https://wordpress.stackexchange.com/...image-232x300.jpg 232w,
            https://wordpress.stackexchange.com/...image-768x994.jpg 768w,
            https://wordpress.stackexchange.com/...image-791x1024.jpg 791w">

Is this the expected output? How should I include full size (…image.jpg in this case) in the srcset attribute?


I noticed that calling <?php echo wp_get_attachment_image( $imageid, 'full', false, $image_attributes); ?> doesn’t include the full size in srcset but still makes use of it if it is needed, as it is the assigned to the src. This seems to work well with browsers that support srcset (I checked network calls and only the necessary image size is called). But I am concerned to assign the full size to the src in browsers that don’t support srcset feature.

1 Answer
1

srcset assumes that all the images specified in the attribute have the same aspect ratio. As it is unlikely that the original (full) image has an aspect ratio that matches the registered image sizes it should be expected that it will never be part of a srcset attribute.

Update from comments: We also have a max width size restriction of 1600px that can be changed via the max_srcset_image_width filter.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *