• How do you specify different “sizes” attributes for two images of different size on the same page? I can’t work out how to create conditional/control logic in wp_calculate_image_sizes and wp_get_attachment_image_attributes.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Those filters pass the requested size (thumbnail, medium, large, etc.) and either attachment ID or attachment object to your callback. Between the two, any data related to the image is available. Whatever your callback returns roughly becomes the return for the related function. What is it that you’re trying to do that WP is not doing satisfactorily by default?

    Thread Starter wpfixer

    (@wpfixer)

    Thank you for your help.
    I’m optimizing image sizes for responsive website where I have some really small images that are a fraction of the screen’s width on mobile screens and larger images both on mobile and desktop. I’m trying to avoid downloading excess/redundant image data.
    Could you please provide a working example with several “sizes” attribute presets and conditional logic to choose specific preset based on some input data?
    I’ve been trying to adapt examples from the web with no luck so far. I’m missing something but I can’t figure out what it is.

    Moderator bcworkz

    (@bcworkz)

    If you upload an image large enough to fill the largest layout, WP will create smaller images at the same time. Then if you echo out the return from wp_get_attachment_image(), WP will automatically use the proper srcset and sizes attributes and the browser will select the optimal image for the size screen. You don’t need to do anything extraordinary.

    If anything, add a custom image size or two to give browsers more of a range of images to choose from. Keep in mind though that for every images size added, WP creates that extra size for all image uploads.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Customize WP core responsive IMG sizes attributes’ is closed to new replies.