wp_get_loading_attr_default
函数
wp_get_loading_attr_default ( $context )
- 参数
-
-
(string)
$context
Context for the element for which the `loading` attribute value is requested.- Required: 是
-
(string)
- 返回值
-
- (string|bool) The default `loading` attribute value. Either ‘lazy’, ‘eager’, or a boolean `false`, to indicate that the `loading` attribute should be skipped.
- 定义位置
-
-
wp-includes/media.php
, line 5428
-
wp-includes/media.php
- 引入
- 5.9.0
- 弃用
- –
获取元素上的`loading`属性的默认值。
只有在通常启用懒惰加载的情况下,才应该为一个标签和上下文调用这个函数。
该函数通常返回 “lazy”,但使用某些启发式方法来猜测当前元素是否有可能出现在页面上方。在这种情况下,它会返回一个布尔值`false`,这将导致`loading`属性被省略。这将导致该元素上的 “loading “属性被省略掉。这一改进的目的是为了避免懒惰地加载初始视口内的元素。视口内的元素,这可能会对性能产生负面影响。
在引擎盖下,该函数每次对主内容中的一个元素调用时都会使用{@see}。的时候都会使用{@see}。如果该元素是第一个内容元素,`loading`属性将被省略。这个默认的省略`loading`属性的阈值是1个内容元素,可以用{@see}来定制。{@see ‘wp_omit_loading_attr_threshold’}过滤器自定义。
function wp_get_loading_attr_default( $context ) { // Only elements with 'the_content' or 'the_post_thumbnail' context have special handling. if ( 'the_content' !== $context && 'the_post_thumbnail' !== $context ) { return 'lazy'; } // 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
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。