利用代碼實(shí)現(xiàn) WordPress 自動(dòng)將 Tag 內(nèi)鏈文章中出現(xiàn)的關(guān)鍵字

麥子 優(yōu)化維護(hù)32字?jǐn)?shù) 1453閱讀4分50秒閱讀模式

從SEO角度考慮,我們是需要適當(dāng)?shù)膶?nèi)容之間貫穿串聯(lián)的,比如文章之間我們可以通過(guò)相關(guān)閱讀、推薦閱讀等方式實(shí)現(xiàn)。同時(shí),對(duì)于內(nèi)鏈Tag關(guān)鍵字我們可以用自動(dòng)關(guān)鍵字內(nèi)鏈插件實(shí)現(xiàn)。但是插件有些時(shí)候也存在一些問(wèn)題,比如安裝插件太多會(huì)導(dǎo)致網(wǎng)站資源占用大,這里我們?nèi)绻褂肳ordPress程序的話,可以使用以下無(wú)插件實(shí)現(xiàn)Tag內(nèi)容文章自動(dòng)內(nèi)鏈效果。

//自動(dòng)TAG內(nèi)鏈效果
$match_num_from = 1;  //一個(gè)關(guān)鍵字少于多少不替換
$match_num_to = 2; //一個(gè)關(guān)鍵字最多替換
//連接到WordPress的模塊
add_filter('the_content','tag_link',1);
//按長(zhǎng)度排序
function tag_sort($a, $b){
if ( $a->name == $b->name ) return 0;
return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}
//改變標(biāo)簽關(guān)鍵字
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;
}

這里我們?cè)陂_(kāi)頭可以設(shè)置頻率。比如最少和最多設(shè)置替換次數(shù)。

如果我們有安裝 LeSEO 插件是可以一鍵設(shè)置自動(dòng)內(nèi)部文章Tag自動(dòng)連接到對(duì)應(yīng)文章實(shí)現(xiàn)內(nèi)聯(lián)效果的。

投上你的一票
 
  • 本文由 麥子 發(fā)表于 2024年9月27日 07:30:31
  • 轉(zhuǎn)載請(qǐng)務(wù)必保留本文鏈接:http://bjj.org.cn/wp-autotags.html
  • WordPress 自動(dòng)Tag