如何为WordPress导航菜单、标签、出站等链接添加nofollow标签属性

  • 时间:2020-05-17 12:22:02
  • 分类:网络文摘
  • 阅读:87 次

在使用wordpress建设网站时,实际上只需要搜索引擎抓取文章链接就可以了,像站内导航、标签、站外等链接过多的被收录,反而会分散网站权重,尤其是文章、页面或评论中的外部出站链接如果失效了还会产生垃圾链接,想要解决这个问题,可以为这些链接添加 nofollow 属性。

nofollow 是一个HTML标签的属性值,由谷歌开创,其作用是“反垃圾链接”,目前已被百度、必应等各大搜索引擎所广泛支持。站长可以通过为链接添加nofollow属性,指示搜索引擎不要追踪(即抓取)网页上的带有nofollow属性的链接,禁止蜘蛛爬行和传递权重,以减少这些链接分散网站权重,这对于网站的seo建设还是很有必要的。

那么在wordpress中应该如何使用nofollow标签属性呢,下面就具体介绍一下:

一、为WordPress导航菜单链接添加 nofollow 属性

1、登陆wordpress后台,进入“外观 > 菜单”页面,点击右上角“显示选项”,在“链接关系(XFN)”处打勾。

为WordPress导航菜单链接添加 nofollow 属性

2、接下来你就可以在菜单结构中看到“链接关系(XFN)”选项了,在此处添加 nofollow ,然后保存即可。

为WordPress导航菜单链接添加 nofollow 属性

二、为WordPress标签链接添加 nofollow 属性

添加如下代码到wordpress主题的functions.php文件中:

  1. function cx_tags() {
  2.     $posttags = get_the_tags();
  3.     if ($posttags) {
  4.         foreach($posttags as $tag) {
  5.             echo '<a class="tag-link' . $tag->term_id . '" href="http://uuxn.com/wordpress-links-add-rel-nofollow/'.get_tag_link($tag).'" rel="nofollow">'.$tag->name.'</a>';
  6.         }
  7.     }
  8. }

然后用

  1. <?php cx_tags(); ?>

替换主题模板中标签标准函数:

  1. <?php the_tags(); ?>

说明:你也可以在 rel="nofollow" 后面添加 target="_blank" 实现在新窗口打开链接。

三、为WordPress文章和页面中的出站链接添加 nofollow 属性

添加如下代码到wordpress主题的functions.php文件中:

  1. add_filter( 'the_content', 'cn_nf_url_parse');
  2. function cn_nf_url_parse( $content ) {
  3.     $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>";
  4.     if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
  5.         if( !emptyempty($matches) ) {
  6.             $srcUrl = get_option('siteurl');
  7.             for ($i=0; $i < count($matches); $i++)
  8.             {
  9.                 $tag = $matches[$i][0];
  10.                 $tag2 = $matches[$i][0];
  11.                 $url = $matches[$i][0];
  12.                 $noFollow = '';
  13.                 $pattern = '/target\s*=\s*"\s*_blank\s*"/';
  14.                 preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
  15.                 if( count($match) < 1 )
  16.                     $noFollow .= ' target="_blank" ';
  17.                 $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
  18.                 preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
  19.                 if( count($match) < 1 )
  20.                     $noFollow .= ' rel="nofollow" ';
  21.                 $pos = strpos($url,$srcUrl);
  22.                 if ($pos === false) {
  23.                     $tag = rtrim ($tag,'>');
  24.                     $tag .= $noFollow.'>';
  25.                     $content = str_replace($tag2,$tag,$content);
  26.                 }
  27.             }
  28.         }
  29.     }
  30.     $content = str_replace(']]>', ']]>', $content);
  31.     return $content;
  32. }

说明:如果已经手动给出站链接添加了 rel="nofollow" 或 target="_blank" 则不会重复添加。

此外,你也可以使用wordpress插件 Nofollow for external link 来实现为出站链接添加 nofollow 属性。

下载地址:http://wordpress.org/plugins/nofollow-for-external-link/

四、为WordPress评论中的出站链接添加 nofollow 属性

添加如下代码到wordpress主题的functions.php文件中:

  1. add_filter('comment_text', 'auto_nofollow'); //nofollow评论内容的站外链接
  2. function auto_nofollow($content) {
  3.     //return stripslashes(wp_rel_nofollow($content));
  4.     return preg_replace_callback('/<a>]+/', 'auto_nofollow_callback', $content);
  5. }
  6. function auto_nofollow_callback($matches) {
  7.     $link = $matches[0];
  8.     $site_link = get_bloginfo('url');
  9.     if (strpos($link, 'rel') === false) {
  10.         $link = preg_replace("%(href=S(?!$site_link))%i", 'rel="nofollow" $1', $link);
  11.     } elseif (preg_match("%href=S(?!$site_link)%i", $link)) {
  12.         $link = preg_replace('/rel=S(?!nofollow)S*/i', 'rel="nofollow"', $link);
  13.     }
  14.     return $link;
  15. }

五、出于特殊需要,你可能想为某个WordPress分类下文章的所有链接添加nofollow属性

添加如下代码到wordpress主题的functions.php文件中:

  1. function nofollow_cat_posts($text) {
  2. global $post;
  3.         if( in_category(1) ) { // 修改这里的分类ID
  4.                 $text = stripslashes(wp_rel_nofollow($text));
  5.         }
  6.         return $text;
  7. }
  8. add_filter('the_content', 'nofollow_cat_posts');

以上就是关于在WordPress中为相关链接添加nofollow属性的介绍,你可以根据自己的需要进行选择。

推荐阅读:
等可能性事件的概率  蜜蜂窝中的数学  数数问题  二月为什么少两天  椭圆的性质  神奇的椭圆  鸡兔同笼公式  统计图的种类和制作步骤  小学统计表知识和制作方法  立体图形的知识和公式 
评论列表
添加评论