I want it so that on a mobile breakpoint, the image in my function uses 'full' and on laptop/desktop it uses 'thumbnail'.

Here's a snippet of what I've got going on:

while ($sponsors->have_posts()) : $sponsors->the_post();
   $id = $sponsors->post->ID;
   $thumb_id = get_post_thumbnail_id( $id );
   $img = get_the_post_thumbnail_url( $id ); // , 'full'
   $src_set = wp_get_attachment_image_srcset( $thumb_id );

   $html_out = '';
   $html_out .= '<img src="' . $img . '" srcset="' . $src_set . '" />';

The srcset appears empty though, not sure how to properly use this.

EDIT: I've also tried doing it with $src = wp_get_attachment_image_url( $thumb_id ); instead of $img, which also produces the same url, but wp_get_attachment_image_srcset still returns false.

  • Have you tried to echo what $thumb_id returns? – Samuel Asor Apr 4 '18 at 17:57
  • @SamuelAsor Yeah, it shows a number that's different from the post id. I dumped $src and using $thumb_id it was able to produce the url using the default thumbnail size. – Darren Apr 4 '18 at 18:03
  • Maybe 'medium' is not a valid image size within your theme, can you try wp_get_attachment_image_srcset( $thumb_id, 'thumbnail' ) ? or any other valid image size : codex.wordpress.org/Function_Reference/… – Pabamato Apr 4 '18 at 19:16
  • @Pabamato it exists, its the default, I see thumbnail, medium, medium-large, and large. – Darren Apr 4 '18 at 21:15
  • @Darren Can't tell too much but I would go for checking wp_calculate_image_srcset() and see when is the ´false´ being returned. Can you see the image in the media library? – Pabamato Apr 4 '18 at 21:38

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.