如何通过Yoast将WordPress搜索结果页robots设为index, follow
解决Yoast SEO修改搜索结果页Robots标签的问题
你遇到的重复指令问题,是因为原有代码的字符串格式不规范,加上Yoast的指令处理逻辑导致的。以下是两种可靠的解决方法:
方式一:使用wpseo_robots过滤器(字符串返回)
直接返回标准格式的Robots指令,覆盖原有值,注意指令间用逗号加空格分隔:
add_filter('wpseo_robots', 'custom_search_robots_tag', 999); function custom_search_robots_tag($robots) { // 判断当前是否为搜索结果页 if (is_search()) { return 'index, follow'; } // 非搜索页保留原有指令 return $robots; }
方式二:使用wpseo_robots_array过滤器(数组返回,更稳妥)
Yoast支持通过数组传递Robots指令,这种方式能彻底避免字符串拼接冲突:
add_filter('wpseo_robots_array', 'custom_search_robots_array', 999); function custom_search_robots_array($robots_array) { if (is_search()) { // 清空原有指令数组 $robots_array = []; // 添加目标指令 $robots_array[] = 'index'; $robots_array[] = 'follow'; } return $robots_array; }
问题原因说明
你之前的代码存在两个问题:一是返回的字符串index,follow没有空格分隔,和Yoast原有指令格式不一致,导致系统错误拼接新旧内容;二是代码里的"是HTML转义的双引号,直接复制到PHP文件会触发语法解析异常,需替换为标准的PHP引号("或')。
内容的提问来源于stack exchange,提问作者Sadraddin Roohani
相关产品推荐
相关产品推荐

