- This topic has 8 replies, 4 voices, and was last updated 4 years, 4 months ago by
David.
-
AuthorPosts
-
May 22, 2021 at 1:50 am #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.
May 22, 2021 at 4:59 am #1793582David
StaffCustomer SupportHi 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-imageclass in the WP Rocket options to exclude it from your lazy loader.June 2, 2021 at 12:23 am #1806854Keshav
Done.
July 20, 2021 at 1:59 am #1862321Sunil
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.
July 20, 2021 at 2:47 am #1862375David
StaffCustomer SupportYou 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
November 26, 2021 at 12:52 am #2022786mariosem
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
November 26, 2021 at 5:33 am #2023266David
StaffCustomer SupportHi 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' );November 26, 2021 at 5:37 am #2023298mariosem
Hi David,
thanks. And what about the class in WP-Rocket settings? Can I keepfirst-featured-imageor do I have to make a change?November 26, 2021 at 7:11 am #2023674David
StaffCustomer SupportNo change to WP rocket, the same class will be added to both images.
-
AuthorPosts
- You must be logged in to reply to this topic.