Archived topic
Preload Featured Image
7 replies · Started by Kelsey on April 21, 2022
Hi there,
I'm trying to preload my featured image in order to speed up my page.
I tried this fix mentioned in another thread:
add_action( 'wp_head', function(){
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
echo '<link rel="preload" as="image" href="'.$featured_img_url.'"/>';
});
Here is one of my pages: https://thegoldenlamb.com/air-fryer-toaster-oven-combos/
But when I do my Google Page Speed test it's still showing up as one of my largest contentful paint images.
Hi there,
the Featured Image is being Lazy Loaded - so its going to load late. What plugin are you using for lazy loading ?
It's my siteground optimizer plugin. Siteground is the host I use and I use their plugin to turn on caching, minify CSS, etc. I also use their plugin to lazy load images.
If you 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 in the SG Optimizer settings > Lazy Load Media - there should be an option to Exclude from lazy load - in the field they provide add: first-featured-image
Thanks David.
I was able to add the PHP snippet, and I found the lazy-load section of the SiteGround Optimizer plugin. But I don't see an area to add the first-featured-image text.
I'm given two options:
1. Exclude CSS Classes from Lazy Load
2. Exclude Media Types from Lazy Load
I don't think the code you provided was CSS.
And there isn't an option under media types to enter my own info, nor is there a "first-featured-image" option. There is a "thumbnails" option but I don't think that's the same.
In the Exclude CSS Classes from Lazy Load field add first-featured-image
Thank you, David :). You're seriously the best. I tell all my friends about Generate Press. The technical help is unparalleled.
Have a great day.
-Kelsey
Thats very kind of you :) Glad to be of help!