给WordPress博客文章添加关键词链接

WordPress博客文章添加关键词链接,没什么好说的,一般都是在用着WP Keyword Link这个插件。
Plugin Name:WP Keyword Link
Description:A SEO plugin that helps you to automatically link keywords to articles.
为你的wordpress博客添加关键词的链接,更多的内链和外链,更好的SEO! 给文章加上内部链接有利于增加搜索引擎收录。完美支持中英文关键词。
如果要安装WP Keyword Link插件,直接后台搜索下载就是了。

最近感觉SeaRu博客的插件越来越多,已经全插件实现了……可怜的CPU又被限制得很低……
所以正在一步步的转为代码实现……可是找了下关键字链接相关代码,也就仅仅一个,还是从WP Keyword Link插件中提取出来的代码。
不管它,好用就行。分享一下,也给自己以后使用备份一下(Ps.你们不都是这样的么?)……

把下列代码稍做修改,放在主题模板函数 (functions.php)倒数第二行即可:

//连接数量
$match_num_from = 1;  //一篇文章中同一个关键字少于多少不秒文本(这个直接填1就好了)
$match_num_to = 4; //一篇文章中同一个关键字最多出现多少次描文本(建议不超过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 = "";
			 $limit = rand($match_num_from,$match_num_to);

			//不连接的 代码
             $content = preg_replace( '|(]+>)(.*)('.$ex_word.')(.*)(]*>)|U'.$case, '$1$2$4$5', $content);
			 $content = preg_replace( '|()|U'.$case, '$1$2$4$5', $content);

				$cleankeyword = preg_quote($cleankeyword,'\'');

					$regEx = '\'(?!((<.*?)|(]*?)>)|([^>]*?))\'s' . $case;

				$content = preg_replace($regEx,$url,$content,$limit);

	$content = str_replace( '', stripslashes($ex_word), $content);

		 }
	 }
    return $content;
}

既然代码从插件而来,肯定会有一部分是无用的,或是做无用功的。这段是百度快照才找到no367.com发布的,但是他那打不开了……至少我没有打开,所以没链接。