首页 文章

如何更新wordpress中的404页元标记

提问于
浏览
0

我正在使用yoast插件为我的网站中的博客wordpress seo默认插件分配下面的元标记为404页面

<meta name="robots" content="noindex,follow"/>

我想将此元标记更新到下面的标记

<meta name="robots" content="noindex,nofollow"/>

我已经浏览了yoast插件文档但是没有找到任何解决方案这可以使用yoast插件本身完成还是以其他任何方式?

2 回答

  • 3

    在WordPress主题的 header.php 文件中,您可以使用下面的代码,该代码使用conditional tagis_404来检查它是否是404页面并打印出您希望的 meta 标签 . 因此,您可以在任何需要的地方使用yoast插件中的选项,如果要为特定页面更改它,则可以使用条件标记 .

    <?php if(is_404()): ?>
    <meta name="robots" content="noindex,nofollow"/>
    <?php endif; ?>
    

    上述解决方案假设Yoast插件未向标头添加任何元标记 . 但是如果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;
    }
    
  • 0

    在插件目录的 frontend/class-frontend.php 中,更改以下内容

    if ( is_search() || is_404() ) {
                    $robots['index'] = 'noindex';
                }
    

    if ( is_search() || is_404() ) {
                $robots['index'] = 'noindex';
                $robots['follow'] = 'nofollow';
            }
    

相关问题