0

I know I have to use wp_get_attachment_image_srcset but I don't know how and where to put it?Any help

<picture>
 <source media="(max-width: 659px)" srcset="inc/images/activity-large.png">
  <source media="(min-width: 660px)" srcset="inc/images/activity-small.png">
  <img src="inc/images/activity-small.png" alt="">
 </picture>
  • Well what does wp_get_attachment_image_srcset() return and where would that fit in your code (aka which values can be replaced)? I'm not sure if this is the method you need, as it only returns a single string with all the srcset in it. You can check out how it works under the hood and replicate that to your needs – kero Jul 26 '18 at 12:28
  • @kero using this code but I don't think it's working <?php // Get this attachment ID $attachment_id = get_post_thumbnail_id( $post_id ); $srcset = wp_get_attachment_image_srcset( $attachment_id,array( 290, 198 ) ); $new = wp_get_attachment_image_sizes( $attachment_id, 'activities-thumb' ); ?><picture> <img src="<?php echo wp_get_attachment_image_url( $attachment_id, 'activities-thumb') ?>" srcset="<?php echo esc_attr( $srcset ); ?>" sizes="(max-width: 659px) , (min-width: 660px)" > </picture> – TestimiUEB Jul 26 '18 at 12:58
  • "I don't think it's working" isn't a proper error description. Which HTML gets produced by this code? Where is it wrong? – kero Jul 26 '18 at 13:28
  • wp_get_attachment_image() will add the appropriate srcset attributes. If you want to use the source tag, then you'd likely need to manually input/enter the URL of the appropriate image. Or get their ID and use it with wp_get_attachment_image_url().. – Sally CJ Jul 26 '18 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.