Retrieve custom taxonomies

时间:2015-05-26 作者:abada henno

我想制作插件,为自定义帖子类型创建自定义分类

喜欢

wp-admin/edit-tags.php?taxonomy=store_cat&post_type=store_kyd_app
并在商店注册此分类法。php

public function __construct($textdomain, $version) {
if (is_admin()) {
add_action(\'init\', array($this, \'create_stores_nonhierarchical_taxonomy\'),1);
}
}
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(\'store_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\' => \'store_cat\' ),
));
}
现在我想用json检索自定义分类法

这是api的代码。php

public function __construct($textdomain, $version) {
add_action(\'init\', array($this, \'json_handler\'),999);
}

public function json_handler() {
  if($_GET[\'endpoint\']=="categories"){
   $this->get_categories();
   }


 }

public function get_categories(){
     $categories = get_terms( \'store_cat\', \'orderby=count&hide_empty=0\' );
    wp_send_json($categories)

}
输出

null
为什么!!

1 个回复
最合适的回答,由SO网友:Milo 整理而成

只有在以下情况下,才能注册分类is_admin 是true,因此对于任何非管理员请求,都不存在您的分类法。必须在每次请求时注册分类法(和帖子类型)。

结束

相关推荐

使用wp_end_json返回Html

我不知道如何通过AJAX正确返回HTML。我有这个惯例function DisplayEventInfoBox() { ?> <div id=\"eventInfo\"> <div class=\"avail-from cart-right\"> <?php and so on... } 如果我在一个AJAX调用中包含这个,并且例程的结尾是die(),那么响应