Archived topic
Featured image show lazy and first image of post not lazy in generepress premium
24 replies · Started by Diogenes on July 8, 2022
Hi.
Yesterday version 2.2.0 of GP premium was released that supposedly fixed this error, but unfortunately it remained.
My setting for the featured image on blog posts is: above the content area.
I mention it, because when you place the featured image (for example) above the title, it works correctly (that is, featured image is not lazy, first image of the post: lazy. But placed: above the content area, the error remains .
I appreciate the comments.
Hmm, strange. It's the same function that serves the image regardless of where you place it. Not sure why placing it outside of the content area would make a difference.
Do you have any custom functions added to the site regarding featured images?
Hi Tom, thanks for replying.
Just like last time, on my test website where I don't have any custom code, the same error occurs.
It seems that the bug is with respect to the featured image of the blog when it is placed in: above the content area
I leave you the credentials as administrator of my test site in the private information area, in case you want to see it for yourself.
Ok, so i been doing a little more digging and testing.
There are many cases as to when WP adds the loading=lazy attribute.
The two instances here are:
1. when using wp_get_attachment_image which GP uses for displaying the featured image.
2. all images within the loop that are past the wp_omit_loading_attr_threshold value which defaults to 1.
So when the featured image is within the loop ( eg. after title ) it gets counted in ( point 2 ) the wp_omit_loading_attr_threshold - so the loading=lazy attribute that would be added because of the wp_get_attachment_image would get removed.
However when the featured image is above the content, it sits outside of the loop.
So it gets the attribute from point #1 but doesn't count for point #2.
To prove this logic, add this PHP Snippet:
add_filter( 'wp_omit_loading_attr_threshold', function($omit_threshold){
if ( is_single() ) {
$omit_threshold = 0;
}
return $omit_threshold;
} );
It will set omit threshold to 0 so all images with the post content should have the loading=lazy attribute.
If that works we just need to look at omitting the attribute from the featured image.
Hi David.
Yes, with that code, the first image of the post is lazy loaded, so that would solve half of the problem.
It would be necessary to remove the lazy load of the featured image, as you comment.
Any help here?.
Any help here?
Hi there,
apologies, i thought i had replied.
Try this snippet:
add_filter( 'wp_get_attachment_image_attributes', 'fp_no_lazy_featured_image', 10, 3 );
function fp_no_lazy_featured_image( $attr ) {
if ( false !== strpos( $attr['class'], 'attachment-full' ) ) {
unset( $attr['loading'] );
}
return $attr;
}
It works perfect, thanks David.
You're welcome
We'll look to see if we can disable that on the Above Content Featured image by default :)