Last active
February 13, 2018 00:13
-
-
Save obelmont/2d94bab896f42ed1b9088f1140a4848d to your computer and use it in GitHub Desktop.
ACF Image Ratio
This file contains hidden or 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
| //Add image ratios to the image array returned through ACF | |
| function imageRatio( $value, $post_id, $field){ | |
| //Only alter array | |
| if ($field['return_format'] === 'array') { | |
| $id = $value['id']; | |
| if( $sizes = get_intermediate_image_sizes() ) { | |
| foreach( $sizes as $size ) { | |
| // url | |
| $src = wp_get_attachment_image_src( $id, $size ); | |
| // Add ratio | |
| $value['sizes'][ $size . '-ratio' ] = ($value['sizes'][ $size . '-height' ] / $value['sizes'][ $size . '-width' ]) * 100; | |
| } | |
| } | |
| } | |
| return $value; | |
| }; | |
| add_filter( 'acf/format_value/type=image', 'imageRatio', 20, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment