做站長(zhǎng)的應(yīng)該都知道,一個(gè)網(wǎng)站的內(nèi)鏈在SEO中也起到很重要的作用,因此我們?cè)诰W(wǎng)站發(fā)布文章時(shí),如果文章有相應(yīng)關(guān)鍵詞出現(xiàn)我們就應(yīng)該給他添加內(nèi)鏈,但是如果我們手動(dòng)加的話有遺漏是一方面,而且還加大了我們的工作量,那么有沒有給文章自動(dòng)加內(nèi)鏈方法,今天小編就給大家?guī)砹藈ordpress發(fā)文章自動(dòng)加內(nèi)鏈方法,修改wordpress主題的functions.php文件(記得是主題文件里面的),并加入以下代碼:
/*
*Wordpress文章關(guān)鍵詞自動(dòng)添加內(nèi)鏈鏈接(tag標(biāo)簽)
*/
//連接數(shù)量
$match_num_from = 1; //一篇文章中同一個(gè)關(guān)鍵字少于多少不錨文本(這個(gè)直接填1就好了)
$match_num_to = 1; //一篇文章中同一個(gè)關(guān)鍵字最多出現(xiàn)多少次錨文本(建議不超過1次)
//連接到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, '$'),__('查看所有文章關(guān)于 %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;
}
/*
*Wordpress文章關(guān)鍵詞自動(dòng)添加內(nèi)鏈鏈接(關(guān)鍵字)
*/
function replace_text_wps($text){
$replace = array(
'改裝' => '<a >改裝</a>',
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter('the_content', 'replace_text_wps');
上面代碼一部分是自動(dòng)加網(wǎng)站TAG標(biāo)簽,一個(gè)是自己想優(yōu)化的詞
未經(jīng)允許不得轉(zhuǎn)載:445IT之家 » wordpress文章自動(dòng)加內(nèi)鏈方法