Created
July 26, 2015 03:13
-
-
Save acodesmith/af99ab9a049559f8e489 to your computer and use it in GitHub Desktop.
srcset shortcode function for wordpress - returns srcset from image attachment id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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