Skip to content

Instantly share code, notes, and snippets.

@jdhobbsuk
Created January 19, 2016 15:04
Show Gist options
  • Save jdhobbsuk/156a75614613c34f0601 to your computer and use it in GitHub Desktop.
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())
/**
* 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