我有一个tag\\u ID=92的标准WordPress页面类别,我不想完全索引该类别中的所有帖子。是否有一种方法可以通过函数中的操作/挂钩来实现这一点。php?
如何对一个目录中的所有WordPress页面设置noindex?
3 个回复
SO网友:Malisa
由于我之前发布的代码对OP不起作用,我们可以尝试使用get_the_category
正如OP所说的,他正在使用YOAST,我将把这个函数包装到机器人的YOAST挂钩中。
add_filter(\'wpseo_robots\', \'yoast_no_home_noindex\', 999);
function yoast_no_home_noindex($string= "") {
$term_id = get_the_category( $post->ID );
if($term_id[0]->term_id == 92) {
$string= "noindex, nofollow";
}
return $string;
}
同样,只需将其放入主题函数文件。SO网友:Malisa
关于noindex,我假设你指的是元机器人noindex,如果是的话,你可以利用in_category 函数在<head></head>
标签,如:
<?php if(in_category( \'92\' ))
echo "\\t<meta name=\'robots\' content=\'noindex, nofollow\' />\\r\\n" ?>
或者,如果您不想直接更改主题,可以将其附加到wp_head 动作挂钩,在函数中放置以下内容。php文件通常位于主主题或子主题文件夹中:add_action(\'wp_head\', \'noRobots\');
function noRobots() {
if(in_category( \'92\' )) echo "\\t<meta name=\'robots\' content=\'noindex, nofollow\' />\\r\\n";
}
如果当前帖子属于ID为“92”的类别,则上述两项都将返回true,也可以与类别名称或slug或其中一个数组一起使用。SO网友:Amine Faiz
在html标题中添加以下代码:
<?php if (is_category(\'92\')): ?>
<meta name="robots" content="noindex,nofollow">
<?php endif ?>