Wordpress > Yoast > 如何调整(或禁用)评论 URL 的元机器人?

问题描述 投票:0回答:2

如果有人能启发我如何在 Yoast 中使用元机器人作为评论网址,那就太好了。

正如您在这些示例 URL 的源代码中看到的:

https://www.bussgeldkatalog.org/geschwindigkeitsueberschreitung/comment-page-2/ https://www.bussgeldkatalog.org/geschwindigkeitsueberschreitung/comment-page-3/

执行了两个相互矛盾的元机器人命令。第一个是“noindex”,而 Yoast 的第二个是“index”。这些评论 URL 应该有一个唯一的元机器人标签,表示“noindex,follow”。

我怎样才能让 Yoast 将其“index, follow”元机器人标签变成“noindex, follow”标签,或者更好的是,仅在评论 URL 上完全禁用 Yoast 元机器人?

在网上冲浪,我发现了一个代码片段可以帮助我解决后者:

add_filter( ‘wpseo_robots’, ‘yoast_seo_robots_remove_single’ );

function yoast_seo_robots_remove_single( $robots ) {
if ( is_single ( 123456 ) ) {
return false;
} else {
return $robots;}
}

问题是我不知道如何将“is_single (123456)”更改为不同的标识符,其中包括所有评论 URL,仅此而已。

期待任何有价值的提示!

保持积极并检测呈阴性, 罗马

wordpress meta-tags yoast
2个回答
1
投票

我这里没什么可说的。

我们可以检索

paginate_comments_links()
并访问
current
键,这应该返回当前评论页码。我没有使用
is_paged()
,因为我无法验证评论页面是否被视为第 n 页(例如:如果它具有
<!--nextpage-->
快速标签)。

我们可以使用

get_comments_number()
来检索当前的评论数。我没有使用
have_comments
,因为我们会在评论循环之外调用它(@see Wordpress have_comments 不起作用)。

<?php

add_filter( 'wpseo_robots', function () {

    if ( is_single() ) {

        $post_id = get_the_ID();
    
        $current_page = paginate_comments_links()['current'];
    
        if ( comments_open( $post_id ) && get_comments_number( get_the_ID() ) > 0 && $current_comment_page > 1 ) {

            return false;
    
        };

    };

};

wpseo_robots
是Yoast用来输出meta标签的钩子。


0
投票

我有一个自定义的 noindex 逻辑函数,最终将它与 Yoast 的过滤器结合起来。

  • 我已经有了一个 noindex 逻辑函数
    is_noindexfollow
    ,允许我基于正则表达式、查询参数
    is_single
    is_page
  • 对 URL 进行条件控制
  • 我将它与酵母过滤器结合在一起,而不是自己将其连接到头部。
  • 由于我找不到删除 yoast 的元机器人标签的方法,因此将我的解决方案融入其中更有意义。

它演示了 OP 如何使用正则表达式模式匹配来处理 is_single 或 is_page 之外的情况。我将它与酵母过滤器结合起来制成:

<?php

add_filter('wpseo_robots', 'yoast_seo_robots_noindex');

// robots is a string 
// - eg. "index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1"

function yoast_seo_robots_noindex($robots)
{
    // dev mode subdomains - to enforce no indexing on dev/staging environments
    $dev_subdomains = [
        'dev',
        'staging',
    ];

    // is the home_url starting with dev or staging?
    $is_dev = in_array(explode('.', parse_url(home_url(), PHP_URL_HOST))[0], $dev_subdomains);

    $noindex = 'noindex, follow';
    $is_no_index = is_noindexfollow(); // 👈 existing noindex logic I had

    if ($is_no_index) {
        return $noindex;
    } else {
        return $is_dev ? $noindex : $robots;
    }
}

function is_noindexfollow()
{
    // Particular Query Data
    $noindex_params = [
        'PageSpeed',
        'pagespeed',
    ];

    if (!empty(array_intersect(array_keys($_GET), $noindex_params))) {
        return true;
    }

    // Regex Pattern match on URI
    $noindex_regexes = [
        //   '/your-path/(option1|option2)/*',
        //   '/oauth/*'
    ];

    $regex = '#' . implode('|', $noindex_regexes) . '#';

    if (!empty($noindex_regexes) && preg_match($regex, $_SERVER['REQUEST_URI'])) {
        return true;
    }

    $noindex_pages = []; // PAGES - use slug names
    $noindex_singles = []; // POSTS - can be ids or slugs

    // If we don't need to check for these, then skip this check.
    if (empty($noindex_pages) && empty($noindex_singles)) {
        return false;
    }

    $isNoIndexPage = !empty($noindex_pages) && is_page($noindex_pages);
    $isNoIndexSingle = !empty($noindex_singles) && is_single($noindex_singles);

    return $isNoIndexPage || $isNoIndexSingle;
}

© www.soinside.com 2019 - 2024. All rights reserved.