WordPress自動投稿タグ付き

1153 ワード

私のブログ
https://www.b2bchain.cn/
 
function.php末尾追加コード
自分の前にラベルライブラリにラベルを設定する必要があります
// 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++;
      }
    }
  }
}