Last active
September 3, 2015 12:04
-
-
Save benjaminpick/29051746fc969f958368 to your computer and use it in GitHub Desktop.
Show an ACF image object Helper Function
This file contains hidden or 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 | |
| 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