Created
January 19, 2016 15:04
-
-
Save jdhobbsuk/156a75614613c34f0601 to your computer and use it in GitHub Desktop.
Get the image URL from ACF, including choice of dimension (from add_image_size())
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
/** | |
* Get ACF Image | |
* | |
* @param string $size – the dimension of the image (using add_image_size() function) | |
* @param string $key – the ACF field key | |
* @param string $post_id – the ID of the post (if empty, will use $post->ID) | |
* @return string The image URL | |
**/ | |
function get_acf_image( $size = 'full', $key = 'image', $post_id = '' ) { | |
if($post_id == ''): | |
global $post; | |
$post_id = $post->ID; | |
endif; | |
$image = get_field($key, $post_id); | |
if( !empty($image) ): | |
if($size != 'full'): | |
$url = $image['sizes'][$size]; | |
if($url == ''): | |
$url = $image['url']; | |
endif; | |
else: | |
$url = $image['url']; | |
endif; | |
return $url; | |
else: | |
return null; | |
endif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment