纯代码实现 WordPress文章TAG标签关键词内链自动优化

不使用插件纯代码实现 WordPress文章TAG标签关键词内链自动优化、不需要借助插件我们就可以实现纯代码实现WP博客的关键词内链自动优化,关键词内链优化有助于SEO网站的一个优化,提高网站的展示,增加网站流量,让你的网站有更多的人访问,虽然有一款很著名的 Auto Tags Link 插件 可以解决这个问题,但是 WordPress 不适用使用过多的插件,这样会降低网站的速度,性能等等,亿破姐这里这里提供一个纯代码版的,大家将以下代码添加到当前主题的 functions.php 文件中就可以了。

纯代码实现 WordPress文章TAG标签关键词内链自动优化

提示:functions.php一般在以下目录 网站/wp-content/themes/当前主题/functions.php

复制//WordPress文章关键词自动内链
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){
	$match_num_from = 1;	//一个标签少于几次不链接
	$match_num_to = 1;	//一个标签最多链接几次
	$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 = "".addcslashes($cleankeyword, '$')."";
			$limit = rand($match_num_from,$match_num_to);
			//不链接代码
			$content = preg_replace( '|(<a[^>]+>)(.*)<pre.*?>('.$ex_word.')(.*)<\/pre>(</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 . ')(?!(([^<>]*?)>)|([^>]*?))\'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);

纯代码实现 WordPress文章TAG标签关键词内链自动优化教程到这里就结束了,以上代码只支持 tag 标签关键词自动内链。

给TA打赏
共{{data.count}}人
人已打赏
便宜VPS

GigsGigsCloud新加坡VPS上线:9.8美元/月,1Gbps带宽/KVM虚拟化

2023-3-16 8:29:30

技术教程

永久免费,无广告,无需VIP,全网影视在线免费看! 点击进入

2022-10-29 11:14:56

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索