一号优惠经验分享网
51福利网的各种薅羊毛福利经验笔记

WordPress纯代码实现自动为文章添加标签及标签内链接+限制标签个数

WordPress 自动为文章添加已使用过的标签

自动为文章内的标签添加内链

51福利网
// WordPress 自动为文章添加已使用过的标签
function array2object($array) { // 数组转对象
  if (is_array($array)) {
    $obj = new StdClass();
    foreach ($array as $key => $val){
      $obj->$key = $val;
    }
  }
  else {
    $obj = $array;
  }
  return $obj;
}
function object2array($object) { // 对象转数组
  if (is_object($object)) {
    foreach ($object as $key => $value) {
      $array[$key] = $value;
    }
  }
  else {
    $array = $object;
  }
  return $array;
}
add_action('save_post', 'auto_add_tags');
function auto_add_tags(){
  $tags = get_tags( array('hide_empty' => false) );
  $post_id = get_the_ID();
  $post_content = get_post($post_id)->post_content;
  if ($tags) {
    $i = 0;
    $arrs = object2array($tags);shuffle($arrs);$tags = array2object($arrs);// 打乱顺序
    foreach ( $tags as $tag ) {
    // 如果文章内容出现了已使用过的标签,自动添加这些标签
      if ( strpos($post_content, $tag->name) !== false){
        if ($i == 5) { // 控制输出数量
          break;
        }
        wp_set_post_tags( $post_id, $tag->name, true );
        $i++;
      }
    }
  }
}







/* 自动为文章内的标签添加内链 */
$match_num_from = 1;        //一篇文章中同一个标签少于几次不自动链接
$match_num_to = 1;      //一篇文章中同一个标签最多自动链接几次
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 = "<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);
                $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($ex_word), $content);
            }
        }
    return $content;
}
add_filter('the_content','tag_link',1);

WordPress纯代码实现自动为文章添加标签及标签内链接+限制标签个数,2个注意改代码合并用

教程1:https://blog.csdn.net/youcijibi/article/details/78366043

教程2:https://blog.csdn.net/biggbang/article/details/81024476

打赏
一号优惠 · 51福利网薅羊毛福利具有时效性,如已失效,请留言
文章名称:《WordPress纯代码实现自动为文章添加标签及标签内链接+限制标签个数》-一号优惠 · 51福利网
免责申明:本站所有活动信息均来自网络,如有失效,不实或侵权,请联系我们删除。谢谢

评论 抢沙发

一号优惠经验分享网最新最全薅羊毛,现金红包线报网

一号优惠经验分享网提供每日最新内部优惠,薅羊毛活动,现金红包领取,免费福利和网赚福利手机赚钱线报,打造中国最受欢迎的网赚信息发布平台!51福利网

51联盟线报群赚钱·合作·帮助

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册