Using a get_field('sampleImage'), how would I go about in order to get the image's width and height using the ACF plug-in? I need to be able to print out the img tag with width and height attributes because of a script.
1 Answer
You can get the ID for the attachment with $attachment_id = get_field('field_name');
. When you have the ID you can use wp_get_attachment_metadata()
to get the info associated with the image in an array. The images width and height are stored in cells named width
and height
.
$attachment_id = get_field('field_name');
$metadata = wp_get_attachment_metadata($attachment_id);
$width = $metadata['width'];
$height = $metadata['height'];
-
Thank you! If you're reading this and looking for a solution - be sure to set the return value of these fields to Attachment ID in the ACF panel. Commented Aug 1, 2012 at 15:10