wp_omit_loading_attr_threshold
云策文档标注
概述
wp_omit_loading_attr_threshold 是一个 WordPress 过滤器,用于控制内容中前几个媒体元素是否添加 lazy-loading 属性。默认情况下,前 3 个媒体元素会省略 loading 属性,以优化页面加载性能。
关键要点
- 过滤器名称:wp_omit_loading_attr_threshold
- 参数:$omit_threshold(整数),指定不添加 loading 属性的媒体元素数量,默认值为 3
- 用途:通过 apply_filters 调用,允许开发者自定义阈值,影响 lazy-loading 行为
- 历史变更:WordPress 6.3.0 版本将默认阈值从 1 改为 3,5.9.0 版本引入此过滤器
- 相关函数:wp_omit_loading_attr_threshold() 用于获取当前阈值
代码示例
function wpdocs_change_lazy_loading_threshold( int $omit_threshold ): int {
/**
* In WordPress 6.3.0 the default threshold was changed from 1 to 3.
* This change is to revert the threshold back to 1.
*/
return 1;
}
add_filter( 'wp_omit_loading_attr_threshold', 'wpdocs_change_lazy_loading_threshold' );注意事项
- 此过滤器仅影响内容中的媒体元素(如图片、视频),不适用于其他元素
- 调整阈值可能影响页面加载速度和用户体验,建议根据具体场景测试
- 默认值变更(从 1 到 3)旨在平衡性能和首屏内容显示,开发者可基于需求覆盖
原文内容
Filters the threshold for how many of the first content media elements to not lazy-load.
Description
For these first content media elements, the loading attribute will be omitted. By default, this is the case for only the very first content media element.
Parameters
$omit_thresholdint-
The number of media elements where the
loadingattribute will not be added. Default 3.
Source
$omit_threshold = apply_filters( 'wp_omit_loading_attr_threshold', 3 );
Skip to note 2 content
Robert C.
Here’s how you could use the
wp_omit_loading_attr_thresholdfilter to change the threshold back to its original value of 1.function wpdocs_change_lazy_loading_threshold( int $omit_threshold ): int { /** * In WordPress 6.3.0 the default threshold was changed from 1 to 3. * This change is to revert the threshold back to 1. */ return 1; } add_filter( 'wp_omit_loading_attr_threshold', 'wpdocs_change_lazy_loading_threshold' );