Last active
August 24, 2016 20:53
-
-
Save timstl/2b4b91137eb80d8a9b3c81b20fc97db8 to your computer and use it in GitHub Desktop.
Function to convert ACF image field into image tag with srcset.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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