當前位置:編程學習大全網 - 源碼下載 - 如何讓文章自動添加標簽鏈接變為內鏈

如何讓文章自動添加標簽鏈接變為內鏈

我們在編寫文章時,經常需要添加壹些標簽關鍵詞的鏈接,這樣不僅可以優化我們的內鏈,對用戶來說也可以參照相關的文章,如果對文章的關鍵字進行手動添加鏈接,那樣對我們來說太麻煩了,而且在標簽關鍵詞很多的情況下我們是記不住的,那怎麽如何讓WordPress站點的文章自動添加標簽鏈接變為內鏈呢?其實我們只需要在主題目錄下的functions.php文件中添加壹段代碼就可以實現了。

打開我們主題的functions.php文件添加如下代碼:

/* 自動為文章內的標簽添加內鏈開始 */

$match_num_from = 1; //壹篇文章中同壹個標簽少於幾次不自動鏈接

$match_num_to = 1; //壹篇文章中同壹個標簽最多自動鏈接幾次

function tag_sort($a, $b){

if ( $a->name == $b->name ) return 0;

return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;

}

function tag_link($content){

global $match_num_from,$match_num_to;

$posttags = get_the_tags();

if ($posttags) {

usort($posttags, "tag_sort");

foreach($posttags as $tag) {

$link = get_tag_link($tag->term_id);

$keyword = $tag->name;

$cleankeyword = stripslashes($keyword);

$url = "<a title="\"".str_replace('%s',addcslashes($cleankeyword," href="file:///D:/%22$link/%22" p="" ?$?),__(?查看含有[%s]標簽的文章?)).?\?;

<a title="\"".str_replace('%s',addcslashes($cleankeyword," href="file:///D:/%22$link/%22" p="" ?$?),__(?查看含有[%s]標簽的文章?)).?\?;  $url .= ' target="_blank"';

<a title="\"".str_replace('%s',addcslashes($cleankeyword," href="file:///D:/%22$link/%22" p="" ?$?),__(?查看含有[%s]標簽的文章?)).?\?;

<a title="\"".str_replace('%s',addcslashes($cleankeyword," href="file:///D:/%22$link/%22" p="" ?$?),__(?查看含有[%s]標簽的文章?)).?\?;  $url .= ">".addcslashes($cleankeyword, '$')."";

$limit = rand($match_num_from,$match_num_to);

$content = preg_replace(

'|(]+>)(.*)('.$ex_word.')(.*)(]*>)|U'.$case,

'$1$2%&&&&&%$4$5', $content);

$content = preg_replace( '|()|U'.$case,

'$1$2%&&&&&%$4$5', $content);

$cleankeyword = preg_quote($cleankeyword,'\'');

$regEx = '\'(?!((<.*?)|(

$content = preg_replace($regEx,$url,$content,$limit);

$content = str_replace( '%&&&&&%',

stripslashes($ex_word), $content);

}

}

return $content;

}

add_filter('the_content','tag_link',1);

/* 自動為文章內的標簽添加內鏈結束 */

把functions.php文件更新覆蓋後,再看看我們的文章,在出現過我們添加的標簽關鍵詞時,是不是就會自動添加鏈接?妳也可以查看本站實例,如《如何美化評論回復的提醒郵件樣式》文章中開頭第壹段的“多說評論”和“評論回復提醒郵件”這兩個詞是不是已經添加了標簽鏈接?

  • 上一篇:在中國新能源汽車競爭格局重塑的壹年,長城汽車將如何排兵布陣?
  • 下一篇:我想練純正的英音,怎麽訓練,有什麽好方法?(希望能說的詳細點)
  • copyright 2024編程學習大全網