Skip to content

Instantly share code, notes, and snippets.

@acodesmith
Created July 26, 2015 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acodesmith/af99ab9a049559f8e489 to your computer and use it in GitHub Desktop.
Save acodesmith/af99ab9a049559f8e489 to your computer and use it in GitHub Desktop.
srcset shortcode function for wordpress - returns srcset from image attachment id
add_shortcode('srcset', 'srcset_func');
function srcset_func($attr)
{
$images = array();
foreach(get_intermediate_image_sizes() as $size){
$images[] = wp_get_attachment_image_src($attr['attachment_id'], $size);
}
$srcset = ''; $count = 0;
foreach($images as $image){ $count++;
$srcset.= $image[0].' '.$image[1]. 'w' . ( $count == count($images) ? '' : ', ' );
}
return 'srcset="'. $srcset .'"';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment