Skip to content

Instantly share code, notes, and snippets.

@timstl
Last active August 24, 2016 20:53
Show Gist options
  • Save timstl/2b4b91137eb80d8a9b3c81b20fc97db8 to your computer and use it in GitHub Desktop.
Save timstl/2b4b91137eb80d8a9b3c81b20fc97db8 to your computer and use it in GitHub Desktop.
Function to convert ACF image field into image tag with srcset.
<?php
/*
Usage: the_field_srcset('field_name');
ACF field should return Image ID.
*/
function the_field_srcset($field, $before='', $after='', $size = 'full', $echo = true) {
if (get_field($field)) {
$img = wp_get_attachment_image(get_field($field), $size);
if ($before) {
$img = $before.$img;
}
if ($after) {
$img = $img.$after;
}
if (!$echo) {
return $img;
}
echo $img;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment