Home » Php » php – How set hover for featured image? How change hover?

php – How set hover for featured image? How change hover?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I would like to set the gif file in the featured image. I would like it to run when I hover over it with the mouse.

I know I should set this in functions.php file

Together with the code:

add_theme_support ('post-thumbnails');

Unfortunately, I have no idea how to do it. I create my blog and I have never done such things. Could someone help me how to do it?
Thank you for your help!

How to&Answers:

When you upload an animated GIF to WordPress, it does all of it’s resizing magic to make the various thumbnail sizes (defaults are thumbnail, medium and large, in addition to the original full). When it does this, the resized versions lose their animation.

If your theme displays featured images at any size other than full, you’ll thusly lose your GIF’s animation.

If you definitely know that you want to display the animated version, though, there’s a little trick you can use.

<?php $thumb_url = get_the_post_thumbnail_url();
      $thumb_low = strtolower($thumb_url);
      if (strpos($thumb_low, '.gif') === false) {
        $thumb_size = 'large';
      } else {
        $thumb_size = 'full';
      } 
?>

What am I doing here is getting the URL of our featured image, then I make sure it’s all lower case letters (WordPress may do this anyway, but just to be safe), then I check to see if .gif is present in the thumbnail’s URL, and finally, if it is, I set the thumbnail size I use to ‘large’. Doing so will allow me to use the original image, which retains it’s animation.

Then, I just need to tell our call to the_post_thumbnail() to use the variable thumbnail size we set, like this:

the_post_thumbnail($thumb_size)

Answer:

If you’ve ever tried to set an animated gif as a featured image in WordPress, you’ve probably discovered that you can’t.

The reason why is pretty simple. Your theme grabs a resized version of the featured image you upload. And in the case of an animated gif, that means it’s not grabbing the original gif image, and so you end up with a static image.

The way around this is pretty simple, however. You can just install a plugin that automatically sets the featured image from the first image in a post. Of course that means you will need to make the animated image the first image, but at least it’s a workaround.

There are a number of plugins that will let you automatically set your first image as the featured image. I tried two and the both worked: Autoset Featured Image and Automatic Featured Image Posts.

Something else I found was that I didn’t even need to download the gif. I could just insert it with the URL.