wp_omit_loading_attr_threshold example


    if ( 'the_content' === $context || 'the_post_thumbnail' === $context ) {
        // Only elements within the main query loop have special handling.         if ( is_admin() || ! in_the_loop() || ! is_main_query() ) {
            return 'lazy';
        }

        // 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;
        }

        // For elements after the threshold, lazy-load them as usual.         return 'lazy';
    }

    // Lazy-load by default for any unknown context.     return 'lazy';
}

if ( 'the_content' === $context || 'the_post_thumbnail' === $context || 'do_shortcode' === $context ) {
        // Only elements within the main query loop have special handling.         if ( is_admin() || ! in_the_loop() || ! is_main_query() ) {
            $loading_attrs['loading'] = 'lazy';
            return $postprocess( $loading_attrs, false );
        }

        // 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, `loading` attribute is omitted.         if ( $content_media_count <= wp_omit_loading_attr_threshold() ) {
            // The first largest image will still get `fetchpriority='high'`.             return $postprocess( $loading_attrs, true );
        }
    }

    // Lazy-load by default for any unknown context.     $loading_attrs['loading'] = 'lazy';
    return $postprocess( $loading_attrs, false );
}

/** * Gets the threshold for how many of the first content media elements to not lazy-load. * * This function runs the {@see 'wp_omit_loading_attr_threshold'} filter, which uses a default threshold value of 3. * The filter is only run once per page load, unless the `$force` parameter is used. * * @since 5.9.0 * * @param bool $force Optional. If set to true, the filter will be (re-)applied even if it already has been before. * Default false. * @return int The number of content media elements to not lazy-load. */
Home | Imprint | This part of the site doesn't use cookies.