如今不論是大型的網站還是個人博客,在乎的不是外鏈接多少決定網站的權重,而是本身網站的內容質量和內鏈接的建設,讓用戶/搜索引擎輕易的找到我們需要的相關內容。內容之間的內鏈建設還是比較重要的,我們可能有些用戶喜歡手工制造,當然這樣很好,如果我們比較怕麻煩,可以通過與WORDPRESS設定的TAG關聯作為內鏈效果。
一般都可以使用插件實現,老蔣一貫比較喜歡使用無插件效果,所以整理到這個無插件實現TAG內鏈效果。
//連接數量
$match_num_from = 1; //一個關鍵字少于多少不替換
$match_num_to = 2; //一個關鍵字最多替換
//連接到WordPress的模塊
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" class="tag_link"';
$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;
}
把上面的代碼腳本,放置到當前WODPRESS主題中的FUNCTIONS.PHP文件中,上面對應的描述參數根據需要修改。比如老蔣博客中的內容TAG內鏈就使用的這種方法。
本文來自老蔣部落:http://www.itbulu.com/wp-in-link.html 轉載注明出處。
新聞熱點
疑難解答