wp_robots

函式
wp_robots ( No parameters )
定義位置
相關方法
wp_no_robotsdo_robotsis_robotswp_roleswp_robots_noindex
引入
5.7.0
棄用
-

wp_robots: 這是一個WordPress過濾鉤子,用於過濾網站的robots.txt檔案的內容。它用於修改robots.txt檔案的內容,以控制搜尋引擎的索引和抓取。

根據需要顯示robots元標記。

使用{@see ‘wp_robots’}篩選器收集robot指令以包括當前上下文。然後對指令進行清理,如果至少有一個相關指令,則輸出robots元標記。

function wp_robots() {
	/**
	 * Filters the directives to be included in the 'robots' meta tag.
	 *
	 * The meta tag will only be included as necessary.
	 *
	 * @since 5.7.0
	 *
	 * @param array $robots Associative array of directives. Every key must be the name of the directive, and the
	 *                      corresponding value must either be a string to provide as value for the directive or a
	 *                      boolean `true` if it is a boolean directive, i.e. without a value.
	 */
	$robots = apply_filters( 'wp_robots', array() );

	$robots_strings = array();
	foreach ( $robots as $directive => $value ) {
		if ( is_string( $value ) ) {
			// If a string value, include it as value for the directive.
			$robots_strings[] = "{$directive}:{$value}";
		} elseif ( $value ) {
			// Otherwise, include the directive if it is truthy.
			$robots_strings[] = $directive;
		}
	}

	if ( empty( $robots_strings ) ) {
		return;
	}

	echo "<meta name='robots' content='" . esc_attr( implode( ', ', $robots_strings ) ) . "' />n";
}

常見問題

FAQs
檢視更多 >