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|bool) The default `loading` attribute value. Either 'lazy', 'eager', or a boolean `false`, to indicate that the `loading` attribute should be skipped.
定義位置
相關方法
wp_get_widget_defaultsget_metadata_defaultwp_omit_loading_attr_thresholdwp_img_tag_add_loading_attr_get_admin_bar_pref
引入
5.9.0
棄用
-

wp_get_loading_attr_default: 這個函式檢索影象的預設載入屬性。它不接受任何引數,並返回一個帶有預設值的字串。

獲取元素上的`loading`屬性的預設值。

只有在通常啟用懶惰載入的情況下,才應該為一個標籤和上下文呼叫這個函式。

該函式通常返回”lazy”,但使用某些啟發式方法來猜測當前元素是否有可能出現在頁面上方。在這種情況下,它會返回一個布林值`false`,這將導致`loading`屬性被省略。這將導致該元素上的”loading”屬性被省略掉。這一改進的目的是為了避免懶惰地載入初始視口內的元素。視口內的元素,這可能會對效能產生負面影響。

在引擎蓋下,該函式每次對主內容中的一個元素呼叫時都會使用{@see}。的時候都會使用{@see}。如果該元素是第一個內容元素,`loading`屬性將被省略。這個預設的省略`loading`屬性的閾值是1個內容元素,可以用{@see}來定製。{@see ‘wp_omit_loading_attr_threshold’}過濾器自定義。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 <= wp_omit_loading_attr_threshold() ) {
return false;
}
// For elements after the threshold, lazy-load them as usual.
return 'lazy';
}
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 <= wp_omit_loading_attr_threshold() ) { return false; } // For elements after the threshold, lazy-load them as usual. return 'lazy'; }
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 <= wp_omit_loading_attr_threshold() ) {
		return false;
	}

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

常見問題

FAQs
檢視更多 >