wordpress博客站最大的问题是:Google收录的页面全是tags页面,而不是文章页。可以通过追加主题函数代码来解决这个问题。 核心原因很简单:标签页(/tag/xxx/)是薄内容 + 大量重复摘要,Google 更容易先抓到它们,却把真正的文章页挤出索引或排到后面。用主题 functions.php 能直接告诉搜索引擎「标签页不要进索引,但可以跟着里面的文章链接走」。 下面这套代码是针对「只追加主题函数」的实用方案,建议加在子主题的 functions.php 末尾(父主题一更新就会丢)。 文章目录 1. 给标签页加 noindex(推荐写法) 2. 标签 Feed 也要挡住(很多人漏这一步) 3. 顺手处理同类垃圾归档(可选) 4. 加完之后必须做的几件事 注意冲突 一、主题函数(子主题 functions.php) 二、robots 协议 三、网站地图 方案 A:用 WordPress 核心地图(无 SEO 插件时) 方案 B:用 SEO 插件(更稳) 方案 C:主题/插件自带的伪地图 上线顺序(避免改完仍收标签) 1. 给标签页加 noindex(推荐写法) WordPress 5.7+ 用 wp_robots 过滤器,比自己 echo <meta> 更干净,也少和 SEO 插件打架。 /** * 标签归档页 noindex, follow * 文章页不受影响 */ add_filter( 'wp_robots', function ( $robots ) { if ( is_tag() ) { $robots['noindex'] = true; unset( $robots['index'] ); // 允许跟踪标签页里的文章链接 $robots['follow'] = true; unset( $robots['nofollow'] ); } return $robots; }, 99 ); 如果主题或插件比较老、不走 wp_robots,再用这个兜底: add_action( 'wp_head', function () { if ( is_tag() ) { echo '<meta name="robots" content="noindex, follow">' . "\n"; } }, 1 ); 2. 标签 Feed 也要挡住(很多人漏这一步) Google 经常把 /tag/xxx/feed/ 收进去,看起来像标签页还在索引里。 add_action( 'template_redirect', function () { if ( is_feed() && is_tag() && ! headers_sent() ) { header( 'X-Robots-Tag: noindex, follow', true ); } } ); 想更狠一点,所有 Feed 都不让索引: add_action( 'template_redirect', function () { if ( is_feed() && ! headers_sent() ) { header( 'X-Robots-Tag: noindex, follow', true ); } } ); 3. 顺手处理同类垃圾归档(可选) 日期归档、作者归档、站内搜索页通常也没价值,可以一起 noindex,把爬虫预算留给文章: add_filter( 'wp_robots', function ( $robots ) { if ( is_tag() || is_date() || is_author() || is_search() ) { $robots['noindex'] = true; unset( $robots['index'] ); $robots['follow'] = true; } return $robots; }, 99 ); 分类页(is_category())先别盲目加。分类如果有描述、文章多,可以当主题入口页,很多站是值得收录的。 4. 加完之后必须做的几件事 用子主题,不要直接改父主题 functions.php。 打开任意标签页 → 查看源代码,确认有 noindex。 打开一篇文章页,确认没有 noindex。 Google Search Console: 用「网址检查」抽几篇重要文章点「请求编入索引」 已收录的标签 URL 可以用「移除」临时清掉,等重新抓取即可 看站点地图里有没有 /tag/。Yoast / Rank Math / AIOSEO 里把 Tags 的「在搜索结果中显示」关掉,并排除出 sitemap。 少打标签。一个标签下面只有 1~2 篇文章,几乎一定是薄内容。 注意冲突 如果你已经装了 Yoast、Rank Math、AIOSEO,优先用插件后台把 Tags 设成 Noindex,上面的代码可能重复或互相覆盖。插件能配就别硬写 PHP。 代码本身只能「阻止标签页进索引」,不能保证文章立刻被收。文章要被收,还得靠:文章能打开、有内链、sitemap 提交、不要被 robots.txt 或「设置 → 阅读 → 建议搜索引擎不要索引」挡住。 一、主题函数(子主题 functions.php) 加在子主题末尾。不要改父主题,更新会丢。 /** * 标签 / 日期 / 作者 / 搜索页:noindex, follow * 文章页、首页、栏目页不处理 */ add_filter( 'wp_robots', function ( $robots ) { if ( is_tag() || is_date() || is_author() || is_search() || is_attachment() ) { $robots['noindex'] = true; unset( $robots['index'] ); $robots['follow'] = true; unset( $robots['nofollow'] ); } return $robots; }, 99 ); /** 老主题不走 wp_robots 时的兜底 */ add_action( 'wp_head', function () { if ( is_tag() || is_date() || is_author() || is_search() || is_attachment() ) { echo '<meta name="robots" content="noindex, follow">' . "\n"; } }, 1 ); /** 标签 Feed、所有 Feed 不收录 */ add_action( 'template_redirect', function () { if ( is_feed() && ! headers_sent() ) { header( 'X-Robots-Tag: noindex, follow', true ); } }, 1 ); /** * 从核心/插件 sitemap 中去掉标签、作者、日期 * WP 5.5+ 核心 sitemap 生效;Rank Math / Yoast 也可能吃这组 filter */ add_filter( 'wp_sitemaps_add_provider', function ( $provider, $name ) { if ( in_array( $name, array( 'users', 'taxonomies' ), true ) ) { return false; // 先全关 taxonomy,下面再只开 category } return $provider; }, 10, 2 ); add_filter( 'wp_sitemaps_taxonomies', function ( $taxonomies ) { unset( $taxonomies['post_tag'] ); return $taxonomies; } ); /** 保险:标签 term 不进 sitemap */ add_filter( 'wp_sitemaps_taxonomies_pre_url_list', function ( $url_list, $taxonomy, $page_num ) { if ( 'post_tag' === $taxonomy ) { return array(); } return $url_list; }, 10, 3 ); 加完后自测: 打开任意 /tag/xxxx,源码里必须有 noindex, follow。 打开任意文章 .html,不能有 noindex。 打开 /tag/xxxx/feed/,响应头应有 X-Robots-Tag: noindex, follow。 如果装了 Yoast / Rank Math / AIOSEO,后台把 Tags → 不显示在搜索结果 / 不进 sitemap 一并关掉,避免插件又把标签加回去。 二、robots 协议 你现在的文件是全站 Allow: /,还指向一堆 ?sitemap219.xml 这种假地图。建议改成: User-agent: * Allow: / # 标签归档不爬(hash 标签页) Disallow: /tag/ Disallow: /tags/ # 无价值归档 Disallow: /author/ Disallow: /author-sitemap Disallow: /*?s= Disallow: /search/ Disallow: /page/ Disallow: */feed/ Disallow: /feed/ Disallow: /comments/feed Disallow: /wp-json/ Disallow: /?replytocom= Disallow: /*?* # 后台与系统 Disallow: /wp-admin/ Allow: /wp-admin/admin-ajax.php Disallow: /wp-includes/ Disallow: /wp-content/plugins/ Disallow: /wp-content/cache/ Disallow: /xmlrpc.php # 只保留一个真正的 XML 地图(改成你生成后的真实地址) Sitemap: https://nirenchuanshuo.com/wp-sitemap.xml 说明: Disallow: /tag/ 是减少浪费爬取。已经收录的标签页还要靠 noindex 才能出索引,只靠 robots 挡不住已收录 URL。 不要 Disallow: /*.html$,文章会一起被挡。 你现在 robots 里那几行 ?sitemapindex.xml、?sitemap219.xml、buy.php?sitemap21.xml 建议全部删掉,那些地址返回的是网页不是地图。 改完后在 Search Console → 抓取 → robots.txt 测试器验证。 三、网站地图 现状:https://nirenchuanshuo.com/?sitemapindex.xml 打开是首页,等于地图失效。 按优先级选一种即可。 方案 A:用 WordPress 核心地图(无 SEO 插件时) 启用后地址一般是: https://nirenchuanshuo.com/wp-sitemap.xml 上面 functions 已经把 post_tag 和用户地图去掉。确认索引里应类似: wp-sitemap-posts-post-1.xml(文章) wp-sitemap-posts-page-1.xml(页面,按需) wp-sitemap-taxonomies-category-1.xml(栏目可以留) 不应再出现 wp-sitemap-taxonomies-post_tag-*.xml。 方案 B:用 SEO 插件(更稳) Yoast / Rank Math / AIOSEO 任选其一: Taxonomies → Tags:不显示在搜索结果 + 不加入 sitemap 作者归档、日期归档同样关掉 用插件给出的 sitemap_index.xml 替换 robots 里的 Sitemap 行 在 GSC 重新提交这个地址 方案 C:主题/插件自带的伪地图 日主题、下载类主题经常用 ?sitemapxxx.xml。这种如果不能输出标准 XML,就关掉,改用核心或 SEO 插件地图,避免 Google 把首页当 sitemap 解析。 提交后检查 XML 源码,每条 <loc> 应是文章或栏目,例如: https://nirenchuanshuo.com/seo/1835.html https://nirenchuanshuo.com/fxjc/wp-syjq 不应出现: https://nirenchuanshuo.com/tag/1870a829d9bc69abf500eca6f00241fe 上线顺序(避免改完仍收标签) 先上 functions.php 的 noindex(已收录标签才能开始出索引)。 再改 robots.txt,挡住继续抓 /tag/。 换掉假 sitemap,GSC 提交新地图。 GSC 用「网址检查」抽几篇重点文章「请求编入索引」。 已收录的 /tag/hash 可在「移除」里批量清一轮(临时),等重新抓取后靠 noindex 长期排除。 后台把文章标签从「几十个」收到 3~8 个,减少新的薄页面。 三件事缺一不可: 函数负责 出索引,robots 负责 少爬,地图负责 告诉 Google 该收哪些文章。只改其中一项,这个站的 hash 标签页还是会继续冒出来。 未经允许不得转载:泥人传说 » wordpress博客站默认就会存在的问题 SEO优化wordpress优化网站优化 分享到: 赞(0)