3

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="...image-232x300.jpg"
    srcset="...image-232x300.jpg 232w,
            ...image-768x994.jpg 768w,
            ...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.

3
  • Do you define the content width in your theme? If this happens to images with the sizer bigger than your content width, then maybe this is where you have a problem.
    – Johansson
    May 20, 2017 at 19:17
  • @JackJohansson the result is the same if I set it, not set it or set it to a really high value.
    – Alvaro
    May 20, 2017 at 20:03
  • I did have your problem a while back, and i had to go with the manual <img src="<?php ... ?> srcset="<?php ... ?>"> at the end.
    – Johansson
    May 20, 2017 at 20:07

1 Answer 1

4

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.

7
  • Thanks for the answer. Why wouldn't the original have the same aspect ratio as the reduced ones if these are not cropped? When the full image size is lower than one of the reduced sizes it does appear in the srcset.
    – Alvaro
    May 20, 2017 at 20:07
  • not sure I understand. Uploaded images have random sizes. the chance that they match any of the defined sizes is small. as for images smaller than the defined sizes... yeh might happen with the way wordpress does things but if it does, it is a bug that should be reported (IMHO the whole feature is badly designed as there is no support for retina and cropped images are mixed with non cropped, but for the usage cases it is designed for it works properly) May 21, 2017 at 2:35
  • 2
    We also have a max width size restriction of 1600 px that can be changed via max_srcset_image_width filter.
    – birgire
    May 21, 2017 at 20:52
  • 1
    @birgire no problem. You are right, I guess raising the limit is a better idea. Thanks for the help.
    – Alvaro
    May 22, 2017 at 10:27
  • 1
    hmm, forgot that detail @birgire, tnx for the edit May 22, 2017 at 11:48

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.