WordPress標簽tag內(nèi)鏈無插件實現(xiàn)
WordPress標簽頁面怎么實現(xiàn)自動內(nèi)鏈呢?研究了幾天,最終才實現(xiàn)了這個tag頁面自動內(nèi)鏈的效果,之前嘗試了插件,但是插件并不好用,而且效果不好。
不需要插件,只需要代碼就可以實現(xiàn)這個功能了,本人親測有效,效果非常好。

只需要在當前主題Functions.php文件中,也可通過后臺-外觀-主題編輯器,進入編輯。
將代碼放在結尾 ?> 的前面,代碼如下:
//自動TAG轉內(nèi)鏈
$match_num_from = 2; // 一個TAG標簽出現(xiàn)幾次才加鏈接
$match_num_to = 1; // 同一個標簽加幾次鏈接
add_filter('the_content','tag_link',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 href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('View all posts in %s'))."\"";
$url .= ' target="_blank"';
$url .= ">".addcslashes($cleankeyword, '$')."</a>";
$limit = rand($match_num_from,$match_num_to);
$content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
$content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
$cleankeyword = preg_quote($cleankeyword,'\'');
$regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;
$content = preg_replace($regEx,$url,$content,$limit);
$content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
}
}
return $content;
}
通過一步操作,就可以實現(xiàn)tag內(nèi)鏈了。
內(nèi)鏈在seo優(yōu)化過程中,非常重要,做足夠多的內(nèi)鏈才能提升網(wǎng)站整體權重,讓網(wǎng)站對搜索引擎更友好。