Disable WordPress 5.5 Lazy Loading Image when Using wp_get_attachment_image()

WordPress 5.5 now has lazy loading for images enabled by default. Tried many things to disable it for specific images, but only this worked. I’ve seen many places recommending to use array(‘loading’ => false) but it didn’t work.

‘loading’ => eager worked instead. It goes like this, when I use ACF Pro loaded image with ID option:

$tab_image = get_sub_field( 'tab_image' );
$size = 'medium';
$loading = ['loading' => 'eager'];

if ( $tab_image ) {
echo wp_get_attachment_image( $tab_image, $size, false, $loading );

It’s important to add false before $loading, or it’ll not work either.

Also possible to write it directly:

echo wp_get_attachment_image( $front_image, $size, false, [
'class' => 'some-class some-other-class', 
'loading' => 'eager'
] );

Now it’s stopping lazy loading for wp_get_attachment_image.

As mentioned here.

This entry was posted in WordPress and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *