[wp-trac] [WordPress Trac] #56588: WordPress puts "loading=lazy" on first image on archive page - wp_get_loading_attr_default bug
WordPress Trac
noreply at wordpress.org
Fri Sep 16 12:34:46 UTC 2022
#56588: WordPress puts "loading=lazy" on first image on archive page -
wp_get_loading_attr_default bug
----------------------------+-----------------------------
Reporter: salvoaranzulla | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Media | Version: 6.0.2
Severity: normal | Keywords:
Focuses: |
----------------------------+-----------------------------
If I see the HTML code of my archive page
https://www.aranzulla.it/internet, I see that the first image has
"loading=lazy" on the main loop, but it should not have it:
{{{
<img width="285" height="200" src="https://www.aranzulla.it/wp-
content/contenuti/2017/02/ffcf69b7-285x200.jpg" class="attachment-home-
bottom size-home-bottom wp-post-image" alt="Come controllare PEC"
loading="lazy" />
}}}
WordPress doesn't put the loading=lazy on the first image using
"wp_get_loading_attr_default" in wp-includes/media.php:
{{{#!php
<?php
// Increase the counter since this is a main query content
element.
$content_media_count = wp_increase_content_media_count();
// If the count so far is below the threshold, return `false` so
that the `loading` attribute is omitted.
if ( $content_media_count <= wp_omit_loading_attr_threshold() ) {
return false;
}
}}}
There is a bug in wp_get_loading_attr_default. If you use the_excerpt(),
the $content_media_count is increased of the number of images inside the
article.
In my archive page, I have:
{{{
<header>
<a href="<?php the_permalink(); ?>"><span
class="heading2"><?php the_title(); ?></span></a>
</header>
<p class="column"><?php the_excerpt(); ?></p>
<figure class="column">
<?php the_post_thumbnail('home-bottom',
array( 'alt' => get_the_title())); ?>
</figure>
}}}
If the first article has 4 photos (that doesn't show in the_excerpt()),
the the_post_thumbnail starts from number 5 and WordPress adds
"loading=lazy".
To reproduce the bug, you can:
1) Edit media.php adding a print_r($content_media_count);:
{{{#!php
<?php
// Increase the counter since this is a main query content
element.
$content_media_count = wp_increase_content_media_count();
print_r($content_media_count); // added to see the counter
// If the count so far is below the threshold, return `false` so
that the `loading` attribute is omitted.
if ( $content_media_count <= wp_omit_loading_attr_threshold() ) {
return false;
}
}}}
2) Create an archive page/homepage where the_excerpt + thumbnail are used
and with few images in that article.
3) When WordPress arrives to the thumbnail, the counter is not 1 even if
it's the first image.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/56588>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list