Last update at :2024-03-24,Edit by888u
The articles published on this site will automatically add internal links to the used tag keywords (tags), which can increase the number of internal links and may also be beneficial to SEO. Some people have asked how this function is implemented before. Today This site will share the specific setting methods.
1. Code sharing
Add the following code to functions.php, where $match_num_from and $match_num_to respectively represent the number of times a tag appears in an article without adding a link and the number of times the same tag adds a link in an article: < /p> //tag -> link $match_num_from = 1; // A tag appears less than the number of times in the article without adding a link $match_num_to = 1; // How many times to add links to the same tag in an article 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; } //Add links to qualified tags 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; //link code $cleankeyword = stripslashes($keyword); $url = "".addcslashes($cleankeyword, '$').""; $limit = rand($match_num_from,$match_num_to); //Code not linked $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; }
2. Effect display
The effect is as follows. This code will automatically add links and descriptions to WordPress tags used in WordPress articles:
Recommended site search: free php space application com domain name registration 1 yuan, independent ip virtual host, China Unicom virtual host, US website domain name, Hong Kong vps, cheap virtual host, website ip address query server rental, free virtual host application, < /p>
发表评论