如何为WooCommerce产品属性归档页添加nofollow代码?
为WooCommerce产品属性归档页添加Nofollow的几种方法
方法1:用WordPress钩子函数(推荐,无需改模板)
直接在主题的functions.php文件或者自定义代码插件里加下面的代码,判断当前页面是产品属性归档页时,自动输出nofollow标签:
add_action('wp_head', 'add_nofollow_to_product_attribute_pages'); function add_nofollow_to_product_attribute_pages() { // 针对指定属性页生效,把pa_organic和pa_gluten-free换成你的属性slug if (is_tax('pa_organic') || is_tax('pa_gluten-free')) { echo '<meta name="robots" content="nofollow, noindex">'; } }
要是想对所有产品属性页都生效,把判断条件换成is_product_taxonomy()就行:
add_action('wp_head', 'add_nofollow_to_all_product_attribute_pages'); function add_nofollow_to_all_product_attribute_pages() { if (is_product_taxonomy()) { echo '<meta name="robots" content="nofollow, noindex">'; } }
方法2:用SEO插件(适合不会写代码的用户)
主流SEO插件比如Yoast SEO、Rank Math都能直接设置:
- Yoast SEO:进入SEO → 搜索外观 → 分类法,找到对应的产品属性,在高级选项里把“允许搜索引擎索引此分类法?”改成“否”,插件会自动添加nofollow标签。
- Rank Math:进入Rank Math → 标题与元数据 → 产品属性,开启“不索引”选项即可。
方法3:修改属性归档模板(需有主题开发基础)
如果你的主题有自定义WooCommerce模板,找到taxonomy-product_cat.php或者通用的taxonomy.php文件,在<head>标签里加这段判断代码:
<?php if (is_product_taxonomy()) : ?> <meta name="robots" content="nofollow, noindex"> <?php endif; ?>
提醒:改主题模板一定要用子主题,不然主题更新后代码会丢失。
内容的提问来源于stack exchange,提问作者Gwame
相关产品推荐
相关产品推荐

