This code example helps set a conditional that will retain the ‘full’ image size within an ACF gallery if the uploaded item is a GIF (thus preserving the animation) but set a thumbnail image size for other file types.
Gallery Field Info
In this example, your ACF gallery field is named (shockingly–) ‘gallery
‘. I’ve set it to grab the thumbnail size but this variable can be specified to another core or custom size of your choosing for your needs.
Full Snippet Example:
<?php
$images = get_field( 'gallery' );
$size = 'thumbnail';
if ( $images ) : ?>
<ul>
<?php foreach ( $images as $image ) :
$url = $image["filename"];
$filetype = wp_check_filetype( $url );
if ( 'gif' == $filetype['ext'] ) {
$url = $image['url'];
} else {
$url = $image['sizes'][$size];
} ?>
<li><img src="<?php echo esc_url( $url ); ?>" alt="Text describing my image here" /></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Note: Remember that you might need to resize your GIF to assist in styling as bringing in the full size of the GIF will retain the animation but ALSO could result in a larger-than-life GIF experience (if there is such a thing…)
Additional Reading:
- WordPress Codex: wp_check_filetype
- ACF Docs: Gallery Field