刚刚设置好\'hierarchical\' => true
在args to中register_taxonomy()
(即使您从未打算添加具有父子关系的术语),在创建/编辑自定义帖子类型时,您将获得与内置类别分类法相同的UI,用于从该自定义分类法分配术语。
如果您想在编辑中下拉术语。php屏幕(如WP Core为“category”自动添加),添加以下内容:
add_action (\'restrict_manage_posts\', \'add_custom_taxonomy_dropdowns\', 10, 2) ;
/**
* add a dropdown/filter to the edit.php screen for our custom taxonomies
*
* @param $post_type string - the post type that is currently being displayed on edit.php screen
* @param $which string - one of \'top\' or \'bottom\'
*/
function
add_custom_taxonomy_dropdowns ($post_type, $which = \'top\')
{
if (\'case_study\' != $post_type) {
return ;
}
$taxonomies = get_object_taxonomies ($post_type, \'object\') ;
foreach ($taxonomies as $tax_obj) {
if ($tax_obj->_builtin) {
// let WP handle the builtin taxonomies
continue ;
}
$args = array (
\'show_option_all\' => $tax_obj->labels->all_items,
\'taxonomy\' => $tax_obj->name,
\'name\' => $tax_obj->name,
\'value_field\' => \'slug\',
\'orderby\' => \'name\',
\'selected\' => isset ($_REQUEST[$tax_obj->name]) ? $_REQUEST[$tax_obj->name] : \'0\',
\'hierarchical\' => $tax_obj->hierarchical,
\'show_count\' => true,
\'hide_empty\' => true,
\'fields\' => \'all\',
) ;
wp_dropdown_categories ($args) ;
}
return ;
}