国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 吉安市| 康马县| 卢湾区| 上饶市| 南城县| 进贤县| 靖宇县| 新晃| 奉贤区| 织金县| 牡丹江市| 林西县| 光泽县| 古丈县| 永仁县| 花莲市| 济源市| 锦屏县| 个旧市| 北流市| 东乡县| 霍林郭勒市| 全州县| 绍兴县| 涞水县| 巴中市| 璧山县| 德保县| 时尚| 济南市| 墨脱县| 麻阳| 左权县| 阳原县| 大连市| 包头市| 繁峙县| 长兴县| 全南县| 邵阳市| 辽宁省|