WordPress SEO优化之如何给文章关键词标签自动添加内链

今天给大家分享一个WordPress SEO优化小技巧,该技巧能够实现文章内的关键词标签自动添加内链

内链的好外,自然就不用说了,它可以让百度蜘蛛在你的网站爬行更多的地方。

当然,很多WordPress插件也可以实现这个功能,但是今天给大家分享的是不用插件给文章关键词标签自动添加内链的方法。

那怎么如何让WordPress站点的文章关键词标签自动添加内链呢?其实我们只需要在主题目录下的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 = "";
			$limit = rand($match_num_from,$match_num_to);
			//不链接代码
			$content = preg_replace( '|(]+>)(.*)('.$ex_word.')(.*)([^>]*>)|U'.$case, '$1$2$4$5', $content);
			$content = preg_replace( '|(WordPress SEO优化之如何给文章关键词标签自动添加内链 - 我爱模板网)|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;
}
add_filter('the_content','tag_link',1);

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
wordpress教程

WordPress打不开官网429 too many requests无法访问和在线安装更新的解决办法

2020-1-6 12:26:19

wordpress教程

WordPress防止F12审查元素扒代码的两种方法

2020-3-14 16:53:14

!
你也想出现在这里?立即 联系我们吧!
信息
个人中心
购物车
优惠劵
今日签到
搜索