Fanly

Fanly

一个摸爬打滚于 IT 互联网的追梦人!

WordPress automatically adds the current post tags as internal links.

Automatically adding internal links in the content of WordPress articles, such as references to article tags. This not only improves the SEO effect of the website, but also enhances the user experience, making it easier for users to find related content on your website. In fact, there are many articles about automatically adding tag internal links to WordPress articles, and almost all of them are the same piece of code. I have been using it, and today I just want to optimize the internal links of the website, so I decided to rewrite the function of automatic internal linking.

The code is actually very simple, but it may look a bit long. This version of the code will replace the text inside the img, a, and code tags with a special marker "%&&&&&%", and then replace the tag text, and finally replace the special marker back to the original tag text. This avoids replacing tag text within these tags. Without further ado, here is the code:

//WordPress article tag automatic internal linking
add_filter('the_content', 'fanly_auto_tags_link');
function fanly_auto_tags_link($content) {
	$tags = get_the_tags(); //Get the tags of the current article
	if($tags){
		foreach ($tags as $tag) {
			$link = get_tag_link($tag->term_id); //Generate tag link
			$tag_name = preg_quote($tag->name, '/'); //Escape tag name
			//Protect the content inside <a>, <img>, <code> tags in advance
			$content = preg_replace_callback('/(<a[^>]*>)(.*?)(</a>)/si', function($matches) use ($tag_name) {
				return str_replace($tag_name, '%&&&&&%', $matches[0]);
			}, $content);
			$content = preg_replace_callback('/(<img[^>]*)(.*?)(' . $tag_name . ')(.*?)(>)/si', function($matches) use ($tag_name) {
				return str_replace($tag_name, '%&&&&&%', $matches[0]);
			}, $content);
			$content = preg_replace_callback('/(<code[^>]*>)(.*?)(</code>)/si', function($matches) use ($tag_name) {
				return str_replace($tag_name, '%&&&&&%', $matches[0]);
			}, $content);
			//Replace the tag text in the content with a link, limiting the replacement to 1 time
			$content = preg_replace('/' . $tag_name . '/iu', '<a href="' . $link . '">' . $tag->name . '</a>', $content, 1);
			//Restore the content inside the protected tags
			$content = str_replace('%&&&&&%', $tag_name, $content);
		}
	}
	return $content;
}

This code will automatically create a link for each tag in the content of the article. First, it gets all the tags of the current article, and then for each tag, it generates a link to the tag archive page. Then, it uses regular expressions to find the tag text in the content and replace it with a link. Here, we use the preg_replace_callback function to avoid replacing the content inside HTML tags.

It looks much more standardized now, but this is just a simple usage and the laziest way to optimize internal links on a website. However, since everyone's usage scenarios are different, I provide a basic solution for everyone to refer to. In fact, you can also enhance it or add custom links.

For more information and questions about WordPress optimization, you can join the QQ group: 255308000

Unless otherwise stated, all articles are original articles from the Tearsnow Blog, and any form of reproduction is prohibited.

Article link: https://zhangzifan.com/wordpress-auto-tags-link.html

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.