Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fitzryland/5882342 to your computer and use it in GitHub Desktop.
Save fitzryland/5882342 to your computer and use it in GitHub Desktop.
Put this function in your functions file and pass the image object from ACF, classes you might want to put on the image, and the WordPress image size name. This function will return an image with the proper src, width, height, title. WordPress, Advanced Custom Fields Plugin
<?php
function custom_acf_image_output($aImg, $class, $size) {
if ($size) {
$widthString = $size . "-width";
$heightString = $size . "-height";
$imgStr = "<img src=\"" . $aImg['sizes'][$size] . "\" alt=\"" . $aImg['title'] . "\" width=\"" . $aImg['sizes'][$widthString] . "\" height=\"" . $aImg['sizes'][$heightString] . "\"";
if ($class) {
$imgStr .= " class=\"" . $class . "\"";
}
$imgStr .= ">";
return $imgStr;
} else {
$imgStr = "<img src=\"" . $aImg['url'] . "\" alt=\"" . $aImg['title'] . "\" width=\"" . $aImg['width'] . "\" height=\"" . $aImg['height'] . "\"";
if ($class) {
$imgStr .= " class=\"" . $class . "\"";
}
$imgStr .= ">";
return $imgStr;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment