I can’t say I’ve tested this but here’s what you could to, it should in theory work.
The wp_get_attachment_image()
functions 4th parameter is an array of attributes. You should be able to pass what you get returned from wp_get_attachment_image_srcset()
to the attribute parameter as follows, sort of similar to the example seen on the docs.
$image_attrs="";
$image_id = 123;
$image_size="full";
$image_srcset = wp_get_attachment_image_srcset( $image_id, $image_size );
if( ! empty( $image_srcset ) ) {
$image_attrs = array(
'srcset' => esc_attr( $image_srcset ),
);
}
$image_html = wp_get_attachment_image( $image_id, $image_size, false, $image_attrs );