分享我所知道的

wordpress免插件自动tag链接

    做过网站的人应该都知道网站内链对于网站的重要性了吧,而且wordpress的标签(tag)的权重是很高的。所以用WP做站,搞好标签链接是很重要的。但是,标签链接很麻烦了,一个一个添加不但非常累,还容易出错。我们可以用插件解决这一问题,但是用插件的弊端大家想必都知道。今天将以下如何免插件实wordpress自动tag链接其实很简单。

代码:

//连接数量
$match_num_from= 1;//一个关键字少于多少不替换
$match_num_to= 10;//一个关键字最多替换
//连接到WordPress的模块
add_filter(‘the_content’,’tag_link’,1);
//按长度排序
functiontag_sort($a,$b){
if($a->name ==$b->name )return0;
return(strlen($a->name) >strlen($b->name) ) ? -1 : 1;
}
//改变标签关键字
functiontag_link($content){
global$match_num_from,$match_num_to;
$posttags= get_the_tags();
if($posttags) {
usort($posttags,”tag_sort”);
foreach($posttagsas$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;
}

将以上代码复制到主题模板函数(functions.php)文件中中,注意是文件的<?php ?>之间。

本文转载自:wp123

赞(0)
未经允许不得转载:追梦人博客 » wordpress免插件自动tag链接
分享到:

评论 抢沙发

评论前必须登录!