wordpress如何自动添加标签锚文本链接(内链)

我们为什么要自动给WordPress文章标签查看更多关于 文章标签 的文章

锚文本呢?答案是不可置疑的,当然是为了SEO的优化。锚文本在网站的SEO优化中占据着很大的比重,合理的关键词锚文本可以给网站的排名带来很大的帮助,大多数建站系统在布局文章关键的锚文本时都要手动去更改、去更新。这样的工作量是非常庞大的,而且效率极其低下。有些聪明的站长可能会利用执行数据库语言来批量的更换关键词的锚文本,对于菜鸟来说数据库操作可能太难,所以就有了这篇教程。

将下面的这个代码加到主题的functions.php文件中 注意是主题的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 = 2;	//一个标签最多链接几次
	$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 的文章'))."\"";
			$url .= ' target="_blank"';
			$url .= ">".addcslashes($cleankeyword, '$')."</a>";
			$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 . ')(?!(([^<>]*?)>)|([^>]*?</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);

 

网站建设

网站被ddos攻击防御的方法手段

2021-12-15 12:10:16

网站建设

网络建站-个人网站备案需要哪些东西

2021-12-15 12:36:03