Last update at :2023-12-11,Edit by888u
I have always been unwilling to mess with the code on this website, and generally just default to whatever the theme is! Every time I see the internal links of my friends’ small sites doing very well, I also want to try hard to achieve such an effect. Since using the xiu theme, I have basically not changed the theme, nor have I made any modifications! However, it was inconvenient to add TAG to the article every time, so I simply searched for it, kept trying, and finally found the implementation of WordPress tag internal linking!
In fact, the powerful plug-in function of WordPress has provided a lot of help to us novices, but too many plug-ins will cause a lot of inconvenience. Generally, plug-ins can be used to achieve this, but this site is also a website that does not only use a few necessary In addition to the plug-in, I am also working on organizing the code, so I organized it into this plug-in-free to achieve the TAG internal linking effect. Just add the following code before the last ?> in the function.php file in the theme folder!
//Number of tag automatic internal connections
$match_num_from = 1; //How many less anchor texts are there for the same keyword in an article (this basically does not need to be modified)
$match_num_to = 1; //The maximum number of times the same keyword can appear in anchor text in an article (it is recommended not to exceed 2 times, I used it once on this site, it depends on your preference)
//Module that connects to WordPress
add_filter(‘the_content’,’tag_link’,1);
//Sort by length
function tag_sort($a, $b){
if ( $a->name == $b->name ) return 0;
return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}
//Change tag keyword
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;
//Connection code
$cleankeyword = stripslashes($keyword);
$url = “ $url .= ‘ target=”_blank” class=”tag_link”‘;
$url .= ">".addcslashes($cleankeyword, ‘$’).””;
$limit = rand($match_num_from,$match_num_to);
//Unconnected code
$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;
}
The current vps recommendation website uses this code. You can check it out. The theme used is the xiu theme. The xiu theme is added to the end of this site!
Recommended site searches: US host rental, overseas virtual host space, local IP address query, free cloud host, mainland China site group server, Korean website domain name, how to register domain name, 6 yuan Alibaba Cloud shared virtual host, Ministry of Industry and Information Technology ICP registration No., Hong Kong hosting,
发表评论