Eager loading to the fisrt image on a slider loop, rest as "lazy"

On a slider with around 20-30 full width images (~150kb each image after compression, on desktop display) I plan to add “eager loading” to the first slide image (and maybe the second) and then let the rest as “lazy”.

The slider is generated from a loop query, there is only one img element, so I can only set “lazy” or “eager” and will apply to all slides. I dont’ see the way to apply different values for slides beyond 2 or 3.

Do you have any idea about how to address this?

Also if I set all the slides as “eager loading”, will this slow down the rest of images of the page?

I’m facing the exact same problem and hope someone has an idea on how to do this!

I’m using an ACF gallery field for the slider and need to add an attribute data-spai-eager="true" to only the in the first slide.

Very late to the party but I found a fix. (Actually two). Both require adding PHP code into either bricks or a snippet plugin.

Note: Optimisation/caching plugins may try and override image management so make sure to check out any image optimisation / lazy loading setting you have.

Option 1.
The first is to add a block of PHP that automates the detection of a looping item such as a slideshow and adds the loading and fetchpriority attributes automatically.

// Automatically detects looping and sets appropritate fetchpriority and loading parameters
add_filter( 'wp_get_attachment_image_attributes', function ( $attr ) {
    // Only target the hero slider image
    if ( empty( $attr['class'] ) || strpos( $attr['class'], 'brxe-scgqtv' ) === false ) {
        return $attr;
    }
    if ( ! class_exists( '\Bricks\Query' ) || ! \Bricks\Query::is_any_looping() ) {
        return $attr;
    }

    if ( \Bricks\Query::get_loop_index() === 0 ) {
        $attr['loading']       = 'eager';
        $attr['fetchpriority'] = 'high';
        $attr['decoding']      = 'sync';
    } else {
        $attr['loading']  = 'lazy';
        $attr['decoding'] = 'async';
        unset( $attr['fetchpriority'] );
    }
    return $attr;
}, 999 );

Option 2.
The second option allows for greater flexibility in defining where you want to use these parameters. To use, Add the PHP code below.

// Force nestable sliders to only load the first images as eager.
function mm_slide_loading() {
return \Bricks\Query::get_loop_index() === 0 ? 'eager' : 'lazy';
}
function mm_slide_priority() {
return \Bricks\Query::get_loop_index() === 0 ? 'high' : 'auto';
}

// Bricks 1.9.7+ requires echo functions to be whitelisted
add_filter( 'bricks/code/echo_function_names', function () {
return [ 'mm_slide_loading', 'mm_slide_priority' ];
} );

The first function takes care of the ‘loading’ parameter by outputting a word ‘eager’ or ‘lazy’ depending on whether it is the first looping item.
The second function takes care of the ‘fetchpriority’ parameter by outputting high or auto depending on whether it is the first slide.

The last add_filter is for Bricks to approve these two functions to run. If you already have this function from other PHP, add the ‘mm_slide_loading’, ‘mm_slide_priority’ to the list of functions already there.

To use these, click on the image and then go to the Attributes section.

Add two attributes.

Name: loading
Value: {echo:mm_slide_loading}

Name: fetchpriority
Value: {echo:mm_slide_priority}

That’s it. Good luck.

(Bricks, if you are reading, please add this to automatically be managed. It’s crazy that we have to go to this extent).