从特定的内部链接中删除noFollow

时间:2019-08-23 作者:Gmac

我的所有网站都有很多内部隐藏链接,带有nofollow属性。

我需要一个函数或过滤器添加到内部functions.php 这将删除nofollow permalink包含特定单词es时的属性"/link/pluto". 你能帮帮我吗?

感谢您的支持。总成

2 个回复
最合适的回答,由SO网友:Mike Baxter 整理而成

我编写了以下代码来解决添加标记属性的问题,但这里有一个版本可以帮助您定位和删除nofollow:

add_filter( \'the_content\', \'ex1_the_content_filter\' );
function ex1_the_content_filter($content) {
    // finds all links in your content with a nofollow
    preg_match_all(\'/\\<a .*?"nofollow".*?a>/\',$content,$matches, PREG_SET_ORDER);

    // loop through all matches
    foreach($matches as $m){
        // potential link to be replaced...
        $toReplace = $m[0];

        // You can add whatever additional "IF" conditions you require to this one
        if (preg_match(\'/.*?\\/pluto.*?/\',$toReplace)){
            // removes rel="nofollow" from the current link
            $replacement = preg_replace(\'/(<a.*?)(rel="nofollow")(.*?a\\>)/\',\'$1$3\',$toReplace);
            // replaces the current link with the $replacement string
            $content = str_ireplace($toReplace,$replacement,$content);
        }
    }
      return $content;
}

SO网友:Gmac

你是我的英雄。我通过与您的代码的内部链接解决了我的问题。我不得不修改它,因为在我的代码rel=“nofollower noopener”和rel=“noopener nofollower”中有许多不同的rel,所以我修改了以下内容:

add_filter( \'the_content\', \'ex1_the_content_filter\' );
function ex1_the_content_filter($content) {

    // finds all links in your content with a nofollow
    preg_match_all(\'/\\<a .*?nofollow.*?a>/\',$content,$matches, PREG_SET_ORDER);

    // loop through all matches
    foreach($matches as $m){
        // potential link to be replaced...
        $toReplace = $m[0];

        // You can add whatever additional "IF" conditions you require to this one
        if (preg_match(\'/.*?\\/link.*?/\',$toReplace)){
            // removes rel="nofollow" from the current link
            $replacement = preg_replace(\'/(<a.*?)(nofollow )(.*?a\\>)/\',\'$1$3\',$toReplace);
            // replaces the current link with the $replacement string
            $content = str_ireplace($toReplace,$replacement,$content);
        }
     }
     return $content;
}

相关推荐

Permalinks setting

所以,我知道这是一个基本的问题,但奇怪的是,它对我不起作用。我想要的:https://example.com/blog/thema/类别名称/postname(用于帖子)https://example.com/blog/thema/类别名称(适用于类别)以下是我的设置:遗憾的是,这行不通。以下是有关在什么设置下工作的详细信息:Posts: /blog/thema/%category%/%postname%/ + Category: blog/thema = 分类工作,帖子获取404Posts: /%cat