Site logo

[Resolved] Add a custom class to featured images

Home Forums Support [Resolved] Add a custom class to featured images

Home Forums Support Add a custom class to featured images

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #2522017
    Muhammad

    Hi there,

    I want to add a custom class to my featured images, so that I can exclude them from lazyloading. I found some solutions through Google but they couldn’t solve my issue. I have added my website URL in private info area for better understanding.

    #2522295
    David
    Staff
    Customer Support

    Hi there,

    is the lazy loading one added by a plugin? If so see here:

    https://generatepress.com/forums/topic/my-page-speed-is-low-how-do-i-fix-it/#post-2276285

    #2522312
    Muhammad

    Hi David,

    Yes, I am using the WPRocket. And where should I put this code?

    function skip_lazy_class_first_featured_image($attr) {
    global $wp_query;
    if ( 0 == $wp_query->current_post ) {
    $attr[‘class’] .= ‘ first-featured-image’;
    }
    return $attr;
    }
    add_filter(‘wp_get_attachment_image_attributes’, ‘skip_lazy_class_first_featured_image’ );

    #2522324
    David
    Staff
    Customer Support

    This doc explains how to add that PHP Snippet:

    https://docs.generatepress.com/article/adding-php/

    It will add the: first-featured-image class to the image.

    #2522339
    Muhammad

    I have installed the code snippets plugin and added+activated the code just now. But still can’t see the first-featured-image class in the source code for the featured image.

    #2522445
    Ying
    Staff
    Customer Support

    Hi there,

    As you are using an image optimization plugin, it changes the image HTML format, the PHP code would not work.

    Can you try adding the class directly to the dynamic image block? I’m not sure if it’s gonna work with your image plugin, but give it a try.

    https://www.screencast.com/t/vhVhOwp89mMq

    Let me know 🙂

    #2522477
    Muhammad

    You’re right, Ying. I added the class on dynamic image block. But now the class the added on the picture tag (the parent to img tag), not the actual img tag.

    #2522494
    Muhammad

    I have changed the settings of Image optimization plugin. Now it’s using the img tag instead of picture tag.

    #2523430
    Ying
    Staff
    Customer Support

    Glad to hear that 🙂

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.