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
查看更多 >