WordPress實(shí)現(xiàn)tag關(guān)鍵詞自動(dòng)內(nèi)鏈 無(wú)插件

admin wordpress評(píng)論9141字?jǐn)?shù) 1360閱讀模式
摘要

給文章中的關(guān)鍵字增加內(nèi)鏈,以優(yōu)化SEO。自動(dòng)內(nèi)鏈的方法有很多,Wordpress插件庫(kù)里面內(nèi)鏈插件也很多,但插件多了對(duì)博客的速度肯定有影響,所以找了個(gè)無(wú)插件實(shí)現(xiàn)的方法。

實(shí)現(xiàn)起來(lái)也很簡(jiǎn)單,只需在主題文件夾中的function.php加入一段代碼即可實(shí)現(xiàn)tag自動(dòng)內(nèi)鏈。
WordPress實(shí)現(xiàn)tag關(guān)鍵詞自動(dòng)內(nèi)鏈 無(wú)插件
WordPress文章關(guān)鍵詞自動(dòng)內(nèi)鏈到相關(guān)標(biāo)簽頁(yè),提高網(wǎng)站SEO,增加用戶粘性
函數(shù)代碼如下:

  1. //WordPress文字標(biāo)簽關(guān)鍵詞自動(dòng)內(nèi)鏈  
  2. $match_num_from = 1;    //一篇文章中同一個(gè)關(guān)鍵字少于多少不錨文本(這個(gè)直接填1就好了)  
  3. $match_num_to = 2;    //一篇文章中同一個(gè)關(guān)鍵字最多出現(xiàn)多少次錨文本(建議不超過(guò)2次)  
  4. function tag_sort($a, $b){  
  5.   if ( $a->name == $b->name ) return 0;  
  6.   return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;  
  7. }  
  8. function tag_link($content){  
  9.   global $match_num_from,$match_num_to;  
  10.    $posttags = get_the_tags();  
  11.    if ($posttags) {  
  12.      usort($posttags, "tag_sort");  
  13.      foreach($posttags as $tag) {  
  14.        $link = get_tag_link($tag->term_id);  
  15.        $keyword = $tag->name;  
  16.        $cleankeyword = stripslashes($keyword);  
  17.        $url = "";  
  18.        $limit = rand($match_num_from,$match_num_to);  
  19.        $content = preg_replace( '|(]+>)(.*)('.$ex_word.')(.*)<\/pre>(]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);  
  20.        $content = preg_replace( '|()|U'.$case, '$1$2%&&&&&%$4$5', $content);  
  21.        $cleankeyword = preg_quote($cleankeyword,'\'');  
  22.        $regEx = '\'(?!((<.*?)|(]*?)>)|([^>]*?))\'s' . $case;  
  23.        $content = preg_replace($regEx,$url,$content,$limit);  
  24.        $content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);  
  25.      }  
  26.    }  
  27.  return $content;  
  28. }  
  29. add_filter('the_content','tag_link',1);  

將以上代碼添加到functions.php文件倒數(shù)第二行,再查看文章,你會(huì)發(fā)現(xiàn)文章中出現(xiàn)的tag標(biāo)簽文字自動(dòng)內(nèi)鏈了,通過(guò)改變代碼中的數(shù)字調(diào)節(jié)內(nèi)鏈個(gè)數(shù)。文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.ykday.cn/7813.html 文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.ykday.cn/7813.html

版權(quán)聲明:文章圖片資源來(lái)源于網(wǎng)絡(luò),如有侵權(quán),請(qǐng)留言刪除!!!
廣告也精彩
admin
  • 本文由 發(fā)表于 2021年9月29日 17:17:26
  • 轉(zhuǎn)載請(qǐng)務(wù)必保留本文鏈接:http://www.ykday.cn/7813.html
匿名

發(fā)表評(píng)論

匿名網(wǎng)友 填寫(xiě)信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: