Skip to content

Instantly share code, notes, and snippets.

@benjaminpick
Last active September 3, 2015 12:04
Show Gist options
  • Select an option

  • Save benjaminpick/29051746fc969f958368 to your computer and use it in GitHub Desktop.

Select an option

Save benjaminpick/29051746fc969f958368 to your computer and use it in GitHub Desktop.
Show an ACF image object Helper Function
<?php
function yt_show_acf_image($img, $opt = array()) {
if (!$img)
return;
$opt_default = array(
'size' => 'xl',
'echo' => true,
'attr' => array(),
'srcset' => true,
);
$opt = $opt + $opt_default;
if (!function_exists('tevkori_get_srcset_string'))
$opt['srcset'] = false;
$tag = $opt['attr'];
if ($opt['size'] && isset($img['sizes'][$opt['size']]) && $img['mime_type'] != 'image/gif') {
$tag['src'] = $img['sizes'][$opt['size']];
$tag['width'] = $img['sizes'][$opt['size'] . '-width'];
$tag['height'] = $img['sizes'][$opt['size'] . '-height'];
} else {
if (WP_DEBUG && $opt['size'] && !isset($img['sizes'][$opt['size']]))
echo 'Unknown size ' . $opt['size'];
$tag['src'] = $img['url'];
$tag['width'] = $img['width'];
$tag['height'] = $img['height'];
}
if ($img['alt']) {
$tag['alt'] = $img['alt'];
}
if (isset($opt['attr']['alt']))
$tag['alt'] = $opt['attr']['alt'];
if ($opt['srcset']) {
unset($tag['src']);
}
$tags = '';
foreach ($tag as $key => $value) {
$tags .= ' ' . $key . '="' .esc_attr($value) . '"';
}
if ($opt['srcset'])
$tags .= ' ' . tevkori_get_srcset_string($img['ID'], @$opt['size'] or 'thumbnail');
$html = "<img $tags />";
if ($opt['echo'])
echo $html;
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment