wp_get_attachment_image_srcset() WordPress Function
The wp_get_attachment_image_srcset() is a WordPress function that allows you to get the image srcset for an attachment. This is useful for responsive images.
wp_get_attachment_image_srcset( int $attachment_id, string|int[] $size = 'medium', array $image_meta = null ) #
Retrieves the value for an image attachment’s ‘srcset’ attribute.
Description
See also
Parameters
- $attachment_id
(int)(Required)Image attachment ID.
- $size
(string|int[])(Optional) Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order).
Default value: 'medium'
- $image_meta
(array)(Optional) The image meta data as returned by 'wp_get_attachment_metadata()'.
Default value: null
Return
(string|false) A 'srcset' value string or false.
Source
File: wp-includes/media.php
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 | 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 ); } |
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
4.4.0 | Introduced. |