加入收藏 | 设为首页 | 会员中心 | 我要投稿 济源站长网 (https://www.0391zz.cn/)- 数据工具、数据仓库、行业智能、CDN、运营!
当前位置: 首页 > 教程 > 正文

wordpress SEO优化教程文章外链自动增加nofollow属性

发布时间:2022-06-24 22:27:48 所属栏目:教程 来源:互联网
导读:做seo的大家都知道如果文章中过多的外链会分散了该页面的权重,这样是不利于网站排名的。 所以网站内页最好是尽量不要链接向外部。但是在某些情况下我们不得不链接向外部,那么该如何处理呢?其实我们可以给外部链接加上nofollow属性,对蜘蛛声明不要爬取这
  做seo的大家都知道如果文章中过多的外链会分散了该页面的权重,这样是不利于网站排名的。
 
  所以网站内页最好是尽量不要链接向外部。但是在某些情况下我们不得不链接向外部,那么该如何处理呢?其实我们可以给外部链接加上nofollow属性,对蜘蛛声明不要爬取这条链接。这样就可以有效的解决权重流失的问题。
 
  但是在编辑文章中一条链接一条链接的加nofollow属性工作量实在太大了,小V就来教大家让wordpress给文章中的外链自动添加nofollow属性。
 
  add_filter( 'the_content', 'seo_wl'); function seo_wl( $content ) { $regexp = "]*href=("??)([^" >]*?)1[^>]*>"; if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) { if( !emptyempty($matches) ) { $srcUrl = get_option('siteurl'); for ($i=0; $i < count($matches); $i++) { $tag = $matches[$i][0]; $tag2 = $matches[$i][0]; $url = $matches[$i][0]; $noFollow = ''; $pattern = '/targets*=s*"s*_blanks*"/'; preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE); if( count($match) < 1 ) $noFollow .= ' target="_blank" '; $pattern = '/rels*=s*"s*[n|d]ofollows*"/'; preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE); if( count($match) < 1 ) $noFollow .= ' rel="nofollow" '; $pos = strpos($url,$srcUrl); if ($pos === false) { $tag = rtrim ($tag,'>'); $tag .= $noFollow.'>'; $content = str_replace($tag2,$tag,$content); } } } } $content = str_replace(']]>', ']]>', $content);
  return $content; } //phpfensi.com
  将以上代码加入到当前主题的functions.php文件即可实现,换主题的时候记得把这段代码加到新主题里噢,不然换主题后文章中的外部链接就会变成无nofollow属性的了.
 
 

(编辑:济源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读