WordPress:在post_save触发时添加新类别

问题描述:

我是wordpress插件开发新手。 当管理员创建新的自定义帖子类型时,我想添加一个新类别。 这里是我的代码:WordPress:在post_save触发时添加新类别

define('POSTTYPE', 'box'); 
add_action('save_post_' . POSTTYPE, array('controller', 
'add_new_category_when_new_box_created'), 10, 3); 

class controller 
{ 
    static function add_new_category_when_new_box_created($post_id, $post, 
$update) 
    { 
    if (get_post_type() == POSTTYPE) { 
     //only new box create category no updated one 
     if ($update) 
      return false; 

     $post_title = $post->post_title; 
     wp_create_category($post_title); 
     return true; 
    } 
    return false; 
    } 
} 

的问题是,$更新始终是真实的,即使当我创造新的岗位。

+0

什么是你的分类名称。 –

+0

@DevDanidhariya我还不熟悉分类学,但可能我没有使用它。 – mjkhonline

参考这个链接,它可能会帮助你https://tommcfarlin.com/programmatically-create-categories/