wp_get_attachment_image_srcset() WP 1.0
Retrieves the value for an image attachment's 'srcset' attribute.
Works based on: wp_calculate_image_srcset(), wp_get_attachment_image_src()
1 time = 0.001658s = very slow | 50000 times = 35.26s = very slow
No Hooks.
Return
String/true/false. A 'srcset' value string or false.
Usage
wp_get_attachment_image_srcset( $attachment_id, $size, $image_meta );
- $attachment_id(int) (required)
- Image attachment ID.
- $size(string/int[])
- Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order).
Default: 'medium' - $image_meta(array)
- The image meta data as returned by 'wp_get_attachment_metadata()'.
Default: null
Notes
- See: wp_calculate_image_srcset()
Changelog
Since 4.4.0 | Introduced. |
Code of wp_get_attachment_image_srcset() wp get attachment image srcset WP 5.6
function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) {
$image = wp_get_attachment_image_src( $attachment_id, $size );
if ( ! $image ) {
return false;
}
if ( ! is_array( $image_meta ) ) {
$image_meta = wp_get_attachment_metadata( $attachment_id );
}
$image_src = $image[0];
$size_array = array(
absint( $image[1] ),
absint( $image[2] ),
);
return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
}