Update (10/11/2016): I added some escaping to the examples. Better safe than sorry.
When creating an image custom field in Advanced Custom Fields, it gives you the option of setting the “Return Value” as an “Image Object”.
If you choose the “Image Object”, get_field()
will get a multidimensional array with all of the image’s values in it (instead of just the URL for the full size version, or the attachment ID), which will look something like this:
$imageArray = get_field('image_field');
var_dump($imageArray);
/* output:
array(9) {
["id"]=>int(5)
["alt"]=>string(4) "Test"
["title"]=>string(2) "29"
["caption"]=>string(0) ""
["description"]=>string(0) ""
["url"]=>string(67) "http://localhost:8888/site/wp-content/uploads/2013/10/29.jpg"
["width"]=>int(1280)
["height"]=>int(960)
["sizes"]=>array(18) {
["thumbnail"]=>string(75) "http://localhost:8888/site/wp-content/uploads/2013/10/29-150x150.jpg"
["thumbnail-width"]=>int(150)
["thumbnail-height"]=>int(150)
["medium"]=>string(75) "http://localhost:8888/site/wp-content/uploads/2013/10/29-300x225.jpg"
["medium-width"]=>int(300)
["medium-height"]=>int(225)
["large"]=>string(76) "http://localhost:8888/site/wp-content/uploads/2013/10/29-1024x768.jpg"
["large-width"]=>int(1024)
["large-height"]=>int(768)
["bones-thumb-600"]=>string(75) "http://localhost:8888/site/wp-content/uploads/2013/10/29-600x150.jpg"
["bones-thumb-600-width"]=>int(600)
["bones-thumb-600-height"]=>int(150)
["bones-thumb-300"]=>string(75) "http://localhost:8888/site/wp-content/uploads/2013/10/29-300x100.jpg"
["bones-thumb-300-width"]=>int(300)
["bones-thumb-300-height"]=>int(100)
["post-thumbnail"]=>string(75) "http://localhost:8888/site/wp-content/uploads/2013/10/29-125x125.jpg"
["post-thumbnail-width"]=>int(125)
["post-thumbnail-height"]=>int(125)
}
}
*/
As you can see, this is a lot of information, which means, power! You can now use all that information to properly display that image in your templates.
To get a specific value, for example the ‘alt’, and ‘thumbnail’ size, you would have the following code:
<?php
$imageArray = get_field('image_field'); // Array returned by Advanced Custom Fields
$imageAlt = esc_attr($imageArray['alt']); // Grab, from the array, the 'alt'
$imageThumbURL = esc_url($imageArray['sizes']['thumbnail']); //grab from the array, the 'sizes', and from it, the 'thumbnail'
?>
<div><img src="<?php echo $imageThumbURL;?>" alt="<?php echo $imageAlt; ?>"></div>
If, for example, you wanted to do a lightbox with your images, you would do this:
<?php
$imageArray = get_field('image_field'); // Array returned by Advanced Custom Fields
$imageAlt = esc_attr($imageArray['alt']); // Grab, from the array, the 'alt'
$imageURL = esc_url($imageArray['url']); // Grab the full size version
$imageThumbURL = esc_url($imageArray['sizes']['thumbnail']); //grab from the array, the 'sizes', and from it, the 'thumbnail'
?>
<a href="<?php echo $imageURL; ?>">
<img src="<?php echo $imageThumbURL;?>" alt="<?php echo $imageAlt; ?>">
</a>
Seriously? This post totally helped me out of a jam! Was trying to return the Image ID of an ACF field. I knew I needed to set the return Value as an Object; but my syntax was off; this post sorted me out. Double hyper extra yay!
Glad I could help! Thanks for the ping back.
It’s funny because I was looking this up just yesterday! And that’s when your article posted. Crazy.
Hey, btw, there are a couple of good WP FB groups, if interested:
https://www.facebook.com/groups/OCWordPress/ and https://www.facebook.com/groups/advancedwp/
You should join up. Tell em Alex sent ya.
Haha! I know! I was surprised when I saw that your comment was not spam, since I had just started the blog, and posted that tutorial.
I’ll check those facebook groups. Thanks.
Awesome!
Pointed me to the right direction 🙂 You should definitely offer ACF to write his original documentation. I still don’t believe that such a useful tip has not even been scratched there…
Thanks!
Thank you so much for the great tutorial! It helped me out of a situation and I spent a large time trying to figure out ACF!
Huge thanks! I’m brand new to ACF and this helped me bunches. I was really confused on how to grab a particular size of an image, then link it to a larger size. You cleared that right up.
You’re welcome!
Thank you so much for this usefull tips!
Ahh… i’ve been trying to figure this out for two days – thanks!
Has anyone found a solution to get this working with ACF Repeater plugin?
I think the Advanced Loop example in the Repeater Documentation does what you want.
Thanks so much for posting this tutorial!
Thank you so much! I’ve been struggling today trying to figure out how to display a custom image size with a full-size lightbox, and this helped immensely.