如何更新 wordpress 中的 404 页面元标记
How to update 404 page meta tag in wordpress
我正在使用 yoast 插件为我网站中的博客 wordpress 进行 seo
默认情况下,插件为 404 页面分配以下元标记
<meta name="robots" content="noindex,follow"/>
我想将此元标记更新为以下元标记
<meta name="robots" content="noindex,nofollow"/>
我已经阅读了 yoast 插件文档
但是没有找到任何解决方案
这可以使用 yoast 插件本身来完成还是有其他方法吗?
在你的 WordPress 主题的 header.php
文件中,你可以使用下面的代码,它使用 conditional tag , is_404 检查它是否是 404 页面并打印出你想要的 meta
标签为了。因此,在任何需要的地方使用 yoast 插件中的选项,如果你想为特定页面更改它,那么你可以使用条件标签。
<?php if(is_404()): ?>
<meta name="robots" content="noindex,nofollow"/>
<?php endif; ?>
以上解决方案假定 Yoast 插件未向 header 添加任何元标记。但是,如果 Yoast 添加了它自己的元标记,那么您可以尝试以下解决方案
将代码添加到 functions.php
文件
add_filter('wpseo_robots', 'yoast_no_home_noindex', 999);
function yoast_no_home_noindex($string= "") {
if (is_404()) {
$string= "noindex,nofollow";
}
return $string;
}
在插件目录的frontend/class-frontend.php
中,更改如下
if ( is_search() || is_404() ) {
$robots['index'] = 'noindex';
}
进入
if ( is_search() || is_404() ) {
$robots['index'] = 'noindex';
$robots['follow'] = 'nofollow';
}
我正在使用 yoast 插件为我网站中的博客 wordpress 进行 seo 默认情况下,插件为 404 页面分配以下元标记
<meta name="robots" content="noindex,follow"/>
我想将此元标记更新为以下元标记
<meta name="robots" content="noindex,nofollow"/>
我已经阅读了 yoast 插件文档 但是没有找到任何解决方案 这可以使用 yoast 插件本身来完成还是有其他方法吗?
在你的 WordPress 主题的 header.php
文件中,你可以使用下面的代码,它使用 conditional tag , is_404 检查它是否是 404 页面并打印出你想要的 meta
标签为了。因此,在任何需要的地方使用 yoast 插件中的选项,如果你想为特定页面更改它,那么你可以使用条件标签。
<?php if(is_404()): ?>
<meta name="robots" content="noindex,nofollow"/>
<?php endif; ?>
以上解决方案假定 Yoast 插件未向 header 添加任何元标记。但是,如果 Yoast 添加了它自己的元标记,那么您可以尝试以下解决方案
将代码添加到 functions.php
文件
add_filter('wpseo_robots', 'yoast_no_home_noindex', 999);
function yoast_no_home_noindex($string= "") {
if (is_404()) {
$string= "noindex,nofollow";
}
return $string;
}
在插件目录的frontend/class-frontend.php
中,更改如下
if ( is_search() || is_404() ) {
$robots['index'] = 'noindex';
}
进入
if ( is_search() || is_404() ) {
$robots['index'] = 'noindex';
$robots['follow'] = 'nofollow';
}