I have a custom post type. Inside loop of single post template I'm trying to show a featured image with custom srcset attribute.

My image sizes are:

add_image_size( 'i600', 600 );
add_image_size( 'i1000', 1000 );
add_image_size( 'i1200', 1200 );
add_image_size( 'i1800', 1800 );

I'm trying this:

$img_id = get_post_thumbnail_id($post->ID);
$img_src = wp_get_attachment_image_url( $img_id, 'i1200' );
$img_srcset = wp_get_attachment_image_srcset( $img_id, array( 'i600', 'i1000', 'i1200' ) );

$img_src returns right URL but var_dump($img_srcset); returns false

Why $img_srcset is returning false?

Aditional info

wp_get_attachment_metadata( $img_id ) returns: https://pastebin.com/pfyFMPvb

WP 4.8

  • It looks like you're feeding the wp_get_attachment_image_srcset() with multiple image sizes. Try a single image size instead or an array of width and height. – birgire Jun 11 '17 at 17:41
  • 1) How is the sizes array notation? I'm trying some unsuccesfully. Can you provide an example? – aitor Jun 12 '17 at 6:38
  • 2) The single image method returns al image sizes in the srcset, including default sizes. Can I get only my custom image sizes? – aitor Jun 12 '17 at 6:41
  • glad to hear you solved it – birgire Jun 12 '17 at 8:43

First, as birgire said, notation was wrong.

Second, I read here that "image sizes matching the aspect ratio for the original image will be returned". So, this is a good behavior for me. I don't need customize srcset anymore.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.