我有自定义分类法
就像那样
/wp管理/编辑标签。php?分类法=my\\u目录(&U);post\\u type=我的诗人
我想要检索json中的分类列表
add_action(\'init\', array($this, \'json_handler\'));
 我试过了
function json_handler(){
$categories = get_terms( \'my_cat\', \'orderby=count&hide_empty=0\' );
print_r($categories);
}
 输出点
WP_Error Object ( [errors:WP_Error:private] => Array ( [invalid_taxonomy] => Array ( [0] => Invalid taxonomy ) ) [error_data:WP_Error:private] => Array ( ) ) 
 但它适用于普通类别
$categories = get_terms( \'category\', \'orderby=count&hide_empty=0\' );
    print_r($categories);
 输出点
Array ( [0] => stdClass Object ( [term_id] => 1 [name] => Uncategorized [slug] => uncategorized [term_group] => 0 [term_taxonomy_id] => 1 [taxonomy] => category [description] => [parent] => 0 [count] => 1 ) )
 这是my\\u cat register\\u分类法
add_action(\'init\', array($this, \'create_stores_nonhierarchical_taxonomy\'));
function create_stores_nonhierarchical_taxonomy() {
// Labels part for the GUI
$labels = array(
\'name\' => _x( \'Store  Categories\', $this -> textdomain ),
\'singular_name\' => _x( \'Store  Categories\', $this -> textdomain ),
\'search_items\' =>  __( \'Search Store  Categories\',$this -> textdomain ),
\'popular_items\' => __( \'Popular Store  Categories\',$this -> textdomain ),
\'all_items\' => __( \'All Store  Categories \',$this -> textdomain ),
\'parent_item\' => null,
\'parent_item_colon\' => null,
\'edit_item\' => __( \'Edit Store Categories\',$this -> textdomain ), 
\'update_item\' => __( \'Update Store Categories\' ,$this -> textdomain),
\'add_new_item\' => __( \'Add New Store Categories\',$this -> textdomain ),
\'new_item_name\' => __( \'New Store Categories Name\',$this -> textdomain ),
\'add_or_remove_items\' => __( \'Add or remove Store Categories\',$this -> textdomain ),
\'choose_from_most_used\' => __( \'Choose from the most used Store Categories\',$this -> textdomain ),
\'menu_name\' => __( \'Store Categories\',$this -> textdomain ),
);
// Now register the non-hierarchical taxonomy like tag
register_taxonomy(\'my_cat\',$this->post_type,array(
\'labels\'            => $labels,
    \'hierarchical\'      => true,
    \'show_ui\'           => true,
    \'how_in_nav_menus\'  => true,
    \'public\'            => true,
    \'show_admin_column\' => true,
    \'query_var\'         => true,
\'rewrite\' => array( \'slug\' => \'my_cat\' ),
));
}
 谢谢
 
                    最合适的回答,由SO网友:cybmeta 整理而成
                    您的问题是,您试图在注册分类法数据之前访问它。
This doesn\'t work:
add_action(\'init\', \'json_handler\');
function json_handler(){
    $categories = get_terms( \'my_cat\', \'orderby=count&hide_empty=0\' );
    if( ! is_wp_error( $categories ) ) {
        // encode the $categories array as json
        print_r( json_encode( $categories ) );
    }
}
add_action(\'init\', \'create_stores_nonhierarchical_taxonomy\');
function create_stores_nonhierarchical_taxonomy() {
    // Labels part for the GUI
    $labels = array(
        \'name\' => _x( \'Store  Categories\', $this -> textdomain ),
        //Rest of your labels
    );
    // Now register the non-hierarchical taxonomy like tag
    register_taxonomy(\'my_cat\',$this->post_type,array(
        \'labels\'            => $labels,
        \'hierarchical\'      => true,
        \'show_ui\'           => true,
        \'how_in_nav_menus\'  => true,
        \'public\'            => true,
        \'show_admin_column\' => true,
        \'query_var\'         => true,
        \'rewrite\'           => array( \'slug\' => \'my_cat\' ),
    ));
}
This works:
add_action(\'init\', \'create_stores_nonhierarchical_taxonomy\');
function create_stores_nonhierarchical_taxonomy() {
    // Labels part for the GUI
    $labels = array(
        \'name\' => _x( \'Store  Categories\', $this -> textdomain ),
        //Rest of your labels
    );
    // Now register the non-hierarchical taxonomy like tag
    register_taxonomy(\'my_cat\',$this->post_type,array(
        \'labels\'            => $labels,
        \'hierarchical\'      => true,
        \'show_ui\'           => true,
        \'how_in_nav_menus\'  => true,
        \'public\'            => true,
        \'show_admin_column\' => true,
        \'query_var\'         => true,
        \'rewrite\'           => array( \'slug\' => \'my_cat\' ),
    ));
}
add_action(\'init\', \'json_handler\');
function json_handler(){
    $categories = get_terms( \'my_cat\', \'orderby=count&hide_empty=0\' );
    if( ! is_wp_error( $categories ) ) {
        // encode the $categories array as json
        print_r( json_encode( $categories ) );
    }
}
 如上所述,更改顺序并不是一个很好的可维护性解决方案。更好的解决方案是:
1.-在上注册分类init 低优先级(默认优先级为10,具有相同优先级的操作按其出现的顺序执行):
add_action( \'init\', array($this, \'create_stores_nonhierarchical_taxonomy\'), 1 );
 2尝试在之后访问分类数据
init 事件,因此您确定分类法已注册,或
init 优先级高于taxonomy register回调优先级的事件,在本例中优先级高于1。
Note: 在之前或之后注册分类init 不推荐使用。