Site logo

[Support request] How to make WP Rocket not to lazy load featured image

Home Forums Support [Support request] How to make WP Rocket not to lazy load featured image

Home Forums Support How to make WP Rocket not to lazy load featured image

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1793445
    Sunil

    Hello

    What class should I use in WP rocket to stop it from lazy loading my featured images as lazy loading featured images increases my LCP.

    #1793582
    David
    Staff
    Customer Support

    Hi there,

    add this PHP Snippet to your site.

    // Add first-featured-image ( or any class ) to featured image of latest post
    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' );

    Then you can use the first-featured-image class in the WP Rocket options to exclude it from your lazy loader.

    #1806854
    Keshav

    Done.

    #1862321
    Sunil

    Hello David

    Sorry for reopening this thread. Can you guide me how I can stop lazy load for the featured image on the AMP pages as well?

    They’re still loaded on amp pages. I use official AMP plugin.

    #1862375
    David
    Staff
    Customer Support

    You would need to check with the AMP plugin developer, as it is ‘swapping’ out the images for its own code, and i don’t know what ( if any ) filters exist for changing their images HTML

    #2022786
    mariosem

    Hello everyone,

    sorry to reopen this thread. Is there a way to modify the snippet in order not to lazy load the first TWO posts?

    Thanks,

    Mario

    #2023266
    David
    Staff
    Customer Support

    Hi there,

    try:

    // Add first-featured-image ( or any class ) to featured image of latest post
    function skip_lazy_class_first_featured_image($attr) {
      global $wp_query;
      if ( 0 == $wp_query->current_post || 1 == $wp_query->current_post ) {
          $attr['class'] .= ' first-featured-image';  
      }
      return $attr;
    }
    add_filter('wp_get_attachment_image_attributes', 'skip_lazy_class_first_featured_image' );
    #2023298
    mariosem

    Hi David,
    thanks. And what about the class in WP-Rocket settings? Can I keep first-featured-image or do I have to make a change?

    #2023674
    David
    Staff
    Customer Support

    No change to WP rocket, the same class will be added to both images.

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