0

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 1

3

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'];
1
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.