Check if WordPress featured image is gif, and output full size image to allow animated gif in Loop

You already know that animated gif in WordPress are also resized to media sizes, so, if your theme do not use “full” featured image size in template, the animated gif will not display correctly.
you can use this code in WordPress loop, to check if the image is a gif, and output in full size that retain the animations frame of the image. Be careful on sizes of .gif files, because it output in full resolution can be slow-down your site.
 

<?php
    $url = wp_get_attachment_url( get_post_thumbnail_id() );
    $filetype = wp_check_filetype($url);
    if ($filetype[ext] == 'gif')
    {the_post_thumbnail('full', array('class' => 'img-responsive')); }
    else
    {the_post_thumbnail('medium', array('class' => 'img-responsive'));}
?>

 

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
adminZach Miller Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Zach Miller
Guest
Zach Miller

Thanks for this block of code. How would I tweak the code to get it to work with my theme’s portfolio post_type featured image? Any help or guidance would be appreciated. 🙂