您现在的位置是:首页 > 网站制作 > WordpressWordpress

WordPress文章自动添加TAG内链代码修复版(跳过文章H1-H4标题) - 搬主题

杰帅2023-06-30【Wordpress】人已围观

简介容易出现H2标题也一起出现TAG内链的情况,不是很完善,这里搬主题对其中正则表达式也不是很熟,所以一直在寻找解决的办法。后来问了一下ChatGPT,终于找到了代码的修复版。

搬主题之前有专门介绍过一个让WordPress文章自动添加TAG内链代码的教程【WordPress给文章自动添加TAG标签图文教程】,网上也有不少的教程,和搬主题给出的代码效果基本是一样的。比如比较有名的是这个:

//WordPress文字标签关键词自动内链  
$match_num_from = 1;    //一篇文章中同一个关键字少于多少不锚文本(这个直接填1就好了)  
$match_num_to = 2;    //一篇文章中同一个关键字最多出现多少次锚文本(建议不超过2次)  
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){  
  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;  
       $cleankeyword = stripslashes($keyword);  
       $url = "";  
       $limit = rand($match_num_from,$match_num_to);  
       $content = preg_replace( '|(]+>)(.*)('.$ex_word.')(.*)<\/pre>(]*>)|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;  
}  
add_filter('the_content','tag_link',1);  

但是以上代码会容易出现H2标题也一起出现TAG内链的情况,不是很完善,这里搬主题对其中正则表达式也不是很熟,所以一直在寻找解决的办法。后来问了一下ChatGPT,终于找到了代码的修复版。

这里要吐槽一下,ChatGPT也不是这么靠谱,经常给出的代码是错误的,你又反馈给它,它又说它弄错了,又重新修正后给你。来来回回很多轮后,才得到如下代码。这里的代码搬主题测试是有效的,但是因为各位的主题或者插件限制,有可能会引起冲突,建议先备份functions.php文件。

将以下修复版添加到WordPress主题的functions.php文件里即可实现WordPress文章自动添加TAG内链代码,并自动跳过H1到H4标题。

//WordPress 自动为文章标签加TAG标签链接
/* 搬主题修复版 www.banzhuti.com */
/* 自动为文章内的标签添加内链开始 */

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){
  $posttags = get_the_tags();
  $match_num_from = 2;  // 一个标签在文章中出现少于多少次不添加链接
  $match_num_to = 5; // 一篇文章中同一个标签添加几次链接

  if ($posttags) {
      usort($posttags, "tag_sort");
      //var_dump($posttags);
      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);
          //不链接的代码
          $pattern = "/<code.*?>(.*?)<\/code>/is"; // 匹配 <code> 标签
          $content = preg_replace_callback(
              $pattern, 
              static function($matches) use ($cleankeyword) {
                  return str_replace($cleankeyword, '%&&&&&%', $matches[0]);
              }, 
              $content
          );
          $title_pattern = "/<(h[1-6]).*?>(.*?)<\/\\1>/is";
          $content = preg_replace_callback(
              $title_pattern, 
              static function($matches) use ($cleankeyword) {
                  return str_replace($cleankeyword, '%&&&&&%', $matches[0]);
              }, 
              $content
          );
          //$content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</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($cleankeyword), $content);
      }
  }
  return $content;
}

add_filter('the_content','tag_link',1);

/* 自动为文章内的标签添加内链结束 */

这个代码在之前的代码基础上做了修改,使用了一个新的正则表达式来跳过H1、H2、H3、H4标题添加TAG标签内链,同时也避免了在标题和链接中重复添加TAG标签内链。

Tags:ChatGPT   SSH教程   WordPress   WordPress SEO优化教程   WordPress教程

很赞哦! ()

文章评论

本站推荐

站点信息

  • 建站时间:2018-10-24
  • 网站程序:帝国CMS7.5
  • 主题模板《今夕何夕》
  • 文章统计1172篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信公众号:扫描二维码,关注我们