Skip to content

Instantly share code, notes, and snippets.

@obelmont
Last active February 13, 2018 00:13
Show Gist options
  • Select an option

  • Save obelmont/2d94bab896f42ed1b9088f1140a4848d to your computer and use it in GitHub Desktop.

Select an option

Save obelmont/2d94bab896f42ed1b9088f1140a4848d to your computer and use it in GitHub Desktop.
ACF Image Ratio
//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