On this page
wp_get_attachment_image_srcset( int $attachment_id, string|int[] $size = 'medium', array $image_meta = null ): string|false
Retrieves the value for an image attachment’s ‘srcset’ attribute.
Description
See also
Parameters
$attachment_idint Required-
Image attachment ID.
$sizestring|int[] Optional-
Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default
'medium'.Default:
'medium' $image_metaarray Optional-
The image meta data as returned by 'wp_get_attachment_metadata() '.
Default:
null
Return
string|false A 'srcset' value string or false.
Source
File: wp-includes/media.php. View all references
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 );
}
Related
Uses
| Uses | Description |
|---|---|
| wp_calculate_image_srcset() wp-includes/media.php | A helper function to calculate the image sources to include in a ‘srcset’ attribute. |
| wp_get_attachment_image_src() wp-includes/media.php | Retrieves an image to represent an attachment. |
| wp_get_attachment_metadata() wp-includes/post.php | Retrieves attachment metadata for attachment ID. |
| absint() wp-includes/functions.php | Converts a value to non-negative integer. |
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_get_attachment_image_srcset