国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > CMS > Wordpress > 正文

WordPress自動(dòng)給文章添加nofollow屬性方法

2024-09-07 00:50:03
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

nofollow屬性是告訴搜索引擎不傳權(quán)重過(guò)去,但WordPressk中如果我們要nofollow屬性就需要手工加了,現(xiàn)在我來(lái)告訴大家利用 Nofollow for external link就可以自動(dòng)給文章添加nofollow屬性了.

直接安裝啟用 Nofollow for external link 插件,或者將下面的代碼添加到當(dāng)前主題的 functions.php 文件即可.

實(shí)例代碼如下:

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

最終效果:自動(dòng)給文章/頁(yè)面的站外鏈接添加nofollow屬性(rel=”nofollow”),并且在新窗口打開(kāi)這些鏈接(即添加 target=”_blank”屬性),如果已經(jīng)手動(dòng)給鏈接添加了 rel=”dofollow”,就不會(huì)添加 rel=”nofollow”,如果手動(dòng)添加了 target=”_blank”,就不會(huì)重復(fù)添加.

為指定分類(lèi)的所有鏈接添加nofollow屬性,那你可以將下面的代碼添加到主題的 functions.php 文件即可:

  1. function nofollow_cat_posts($text) { 
  2. global $post
  3.         if( in_category(1) ) { // 修改這里的分類(lèi)ID 
  4.                 $text = stripslashes(wp_rel_nofollow($text)); 
  5.         } 
  6.         return $text
  7. add_filter('the_content''nofollow_cat_posts'); 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东丰县| 垣曲县| 宝鸡市| 榕江县| 沈阳市| 旺苍县| 稷山县| 太谷县| 清水河县| 开封县| 双城市| 桃园市| 凌云县| 丰城市| 枞阳县| 延安市| 成安县| 双桥区| 阿拉善盟| 安仁县| 靖边县| 竹北市| 五大连池市| 开鲁县| 枣庄市| 偃师市| 广元市| 新乡市| 大理市| 米易县| 宜都市| 徐闻县| 泰顺县| 乌海市| 长宁区| 开平市| 府谷县| 察雅县| 收藏| 睢宁县| 大理市|