Wordpress 添加 标签关键词自动内链


创建WP_Keyword_Link.php文件,将下列代码复制进去,上传入plugin目录,后台启用即可

<?php
/*
Plugin Name: 关键词自动内链
Description: 文章内的关键词标签自动添加内链。
Version: 1.0
Author: artcg.design
Author URI: https://artcg.design
*/
//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 = 1;  //一个标签最多链接几次
  $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);
原文链接:https://www.artcg.design/1447.html,转载请注明出处。
0

评论0

请先

站点公告

  1. 1.请不要使用QQ浏览器有可能无法访问.
  2. ————————————————
  3. 2.邮箱注册时,验证码可能归类在垃圾箱里,请注意查收
  4. ————————————————
  5. 3.如果有问题,请添加微信qifengct客服QQ:1924173440咨询
  6. ————————————————
显示验证码
没有账号?注册  忘记密码?