純代碼為WordPress文章內(nèi)關(guān)鍵字加上內(nèi)鏈鏈接的方式方法

[重要通告]如您遇疑難雜癥,本站支持知識(shí)付費(fèi)業(yè)務(wù),掃右邊二維碼加博主微信,可節(jié)省您寶貴時(shí)間哦!

WordPress文章關(guān)鍵詞自動(dòng)添加內(nèi)鏈鏈接,代碼插件皆可實(shí)現(xiàn),可是本著少用插件的原則,我們還是用代碼比較合適;

這里有幾種方式,大家可以都測(cè)試幾下:

WordPress純代碼實(shí)現(xiàn)自動(dòng)添加文章標(biāo)簽的實(shí)現(xiàn)方法:只需將以下代碼添加到當(dāng)前主題functions.php文件最后即可。

/* 自動(dòng)為文章添加標(biāo)簽 起始*/
add_action('save_post', 'auto_add_tags');
function auto_add_tags(){
$tags = get_tags( array('hide_empty' => false) );
$post_id = get_the_ID();
$post_content = get_post($post_id)->post_content;
if ($tags) {
foreach ( $tags as $tag ) {
// 如果文章內(nèi)容出現(xiàn)了已使用過的標(biāo)簽,自動(dòng)添加這些標(biāo)簽
if ( strpos($post_content, $tag->name) !== false)
wp_set_post_tags( $post_id, $tag->name, true );
}
}
}/* 自動(dòng)為文章添加標(biāo)簽 結(jié)束*/

以上代碼功能就是在我們發(fā)布/保存/更新文章時(shí),自動(dòng)檢測(cè)文章中的內(nèi)容,是否出現(xiàn)曾經(jīng)使用過的標(biāo)簽。如果出現(xiàn)過就會(huì)自動(dòng)為文章添加這些標(biāo)簽哦;

WordPress純代碼實(shí)現(xiàn)自動(dòng)為文章內(nèi)的標(biāo)簽添加內(nèi)鏈的方法同樣是將以下代碼添加到當(dāng)前主題的functions.php文件最后即可。

/*
*Wordpress文章關(guān)鍵詞自動(dòng)添加內(nèi)鏈鏈接代碼
*http://kr288.com/?p=3382
*/
$match_num_from = 1; //一篇文章中同一個(gè)標(biāo)簽少于幾次不自動(dòng)鏈接
$match_num_to = 1; //一篇文章中同一個(gè)標(biāo)簽最多自動(dòng)鏈接幾次
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, '$'),__('【查看含有[%s]標(biāo)簽的文章】'))."\"";
$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;
}
add_filter('the_content','tag_link',1);

有時(shí),我們希望wordpress文章能有指定關(guān)鍵詞指向首頁或其它我們重點(diǎn)要推廣的頁面。屆時(shí),可以給wordpress主題添加指定關(guān)鍵詞內(nèi)鏈。代碼如下:

//指定關(guān)鍵詞內(nèi)鏈開始
function content_keywords_link($text){
$replace = array(
'老梁`s Blog' => '<a href="http://kr288.com/" rel="bookmark" title="老梁`s Blog">老梁`s Blog</a>',
'財(cái)務(wù)軟件' => '<a href="http://kr288.com/" rel="bookmark" title="財(cái)務(wù)軟件">財(cái)務(wù)軟件</a>',
'企業(yè)網(wǎng)站建設(shè)方案' => '<a href="http://kr288.com/" rel="bookmark" title="企業(yè)網(wǎng)站建設(shè)方案">企業(yè)網(wǎng)站建設(shè)方案</a>'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter('the_content', 'content_keywords_link');

//指定關(guān)鍵詞內(nèi)鏈結(jié)束

還一種寫法,代碼比較少

/* 自動(dòng)為文章內(nèi)的標(biāo)簽添加內(nèi)鏈開始 https://www06929.com*/
$match_num_from = 1; ? ? ? ?//一篇文章中同一個(gè)標(biāo)簽少于幾次不自動(dòng)鏈接
$match_num_to = 1; ? ? ?//一篇文章中同一個(gè)標(biāo)簽最多自動(dòng)鏈接幾次
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, '$'),__('【查看含有[%s]標(biāo)簽的文章】')).""";
? ? ? ? ? ? ? ? $url .= ' target="_blank"';
? ? ? ? ? ? ? ? $url .= " rel="noopener noreferrer">".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;
}
add_filter('the_content','tag_link',1);
/* 自動(dòng)為文章內(nèi)的標(biāo)簽添加內(nèi)鏈結(jié)束 */

2021年0921更新;

考慮到每次輸出都是標(biāo)簽庫里面的前幾個(gè)標(biāo)簽不利于內(nèi)容編排SEO,所以又增加了文章內(nèi)容中所有標(biāo)簽打亂功能的高級(jí)增強(qiáng)版代碼如下。

// WordPress 自動(dòng)為文章添加已使用過的標(biāo)簽
function array2object($array) { // 數(shù)組轉(zhuǎn)對(duì)象
if (is_array($array)) {
$obj = new StdClass();
foreach ($array as $key => $val){
$obj->$key = $val;
}
}
else {
$obj = $array;
}
return $obj;
}
function object2array($object) { // 對(duì)象轉(zhuǎn)數(shù)組
if (is_object($object)) {
foreach ($object as $key => $value) {
$array[$key] = $value;
}
}
else {
$array = $object;
}
return $array;
}
add_action('save_post', 'auto_add_tags');
function auto_add_tags(){
$tags = get_tags( array('hide_empty' => false) );
$post_id = get_the_ID();
$post_content = get_post($post_id)->post_content;
if ($tags) {
$i = 0;
$arrs = object2array($tags);shuffle($arrs);$tags = array2object($arrs);// 打亂順序
foreach ( $tags as $tag ) {
// 如果文章內(nèi)容出現(xiàn)了已使用過的標(biāo)簽,自動(dòng)添加這些標(biāo)簽
if ( strpos($post_content, $tag->name) !== false){
if ($i == 5) { // 控制輸出數(shù)量
break;
}
wp_set_post_tags( $post_id, $tag->name, true );
$i++;
}
}
}
}

問題未解決?付費(fèi)解決問題加Q或微信 2589053300 (即Q號(hào)又微信號(hào))右上方掃一掃可加博主微信

所寫所說,是心之所感,思之所悟,行之所得;文當(dāng)無敷衍,落筆求簡(jiǎn)潔。 以所舍,求所獲;有所依,方所成!

支付寶贊助
微信贊助

免責(zé)聲明,若由于商用引起版權(quán)糾紛,一切責(zé)任均由使用者承擔(dān)。

您必須遵守我們的協(xié)議,如您下載該資源,行為將被視為對(duì)《免責(zé)聲明》全部?jī)?nèi)容的認(rèn)可->聯(lián)系老梁投訴資源
LaoLiang.Net部分資源來自互聯(lián)網(wǎng)收集,僅供用于學(xué)習(xí)和交流,請(qǐng)勿用于商業(yè)用途。如有侵權(quán)、不妥之處,請(qǐng)聯(lián)系站長(zhǎng)并出示版權(quán)證明以便刪除。 敬請(qǐng)諒解! 侵權(quán)刪帖/違法舉報(bào)/投稿等事物聯(lián)系郵箱:service@laoliang.net
意在交流學(xué)習(xí),歡迎贊賞評(píng)論,如有謬誤,請(qǐng)聯(lián)系指正;轉(zhuǎn)載請(qǐng)注明出處: » 純代碼為WordPress文章內(nèi)關(guān)鍵字加上內(nèi)鏈鏈接的方式方法

發(fā)表回復(fù)

本站承接,網(wǎng)站推廣(SEM,SEO);軟件安裝與調(diào)試;服務(wù)器或網(wǎng)絡(luò)推薦及配置;APP開發(fā)與維護(hù);網(wǎng)站開發(fā)修改及維護(hù); 各財(cái)務(wù)軟件安裝調(diào)試及注冊(cè)服務(wù)(金蝶,用友,管家婆,速達(dá),星宇等);同時(shí)也有客戶管理系統(tǒng),人力資源,超市POS,醫(yī)藥管理等;

立即查看 了解詳情