wp_increase_content_media_count example

/* * The first elements in 'the_content' or 'the_post_thumbnail' should not be lazy-loaded, * as they are likely above the fold. */
    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.
        if ( isset( $loading_attributes['loading'] ) && ! wp_lazy_loading_enabled( $tag_name$context ) ) {
            unset( $loading_attributes['loading'] );
        }
        return $loading_attributes;
    };
    // Closure to increase media count for images with certain minimum threshold, mostly used for header images.     $maybe_increase_content_media_count = static function() use ( $attr ) {
        /** This filter is documented in wp-includes/media.php */
        $wp_min_priority_img_pixels = apply_filters( 'wp_min_priority_img_pixels', 50000 );
        // Images with a certain minimum size in the header of the page are also counted towards the threshold.         if ( $wp_min_priority_img_pixels <= $attr['width'] * $attr['height'] ) {
            wp_increase_content_media_count();
        }
    };

    $loading_attrs = array();

    /* * Skip lazy-loading for the overall block template, as it is handled more granularly. * The skip is also applicable for `fetchpriority`. */
    if ( 'template' === $context ) {
        return $loading_attrs;
    }
Home | Imprint | This part of the site doesn't use cookies.