限制wordpress中自定义帖子类型的类别

时间:2015-10-19 12:36:46

标签: php wordpress custom-post-type taxonomy

我创建了一个用于添加员工的自定义帖子类型。我为它添加了类别分类。然后我添加了一个名为" Employees" (slug:员工)。但是当我添加一名新员工时,它会向我显示所有要选择的类别。如何限制它仅显示"员工"这里的类别?

这是我在functions.php中的php代码:

function employees_register() {
    $labels = array(
        'name' => _x('Employees', 'post type general name'),
        'singular_name' => _x('Employee', 'post type singular name'),
        'add_new' => _x('Add New', 'employees'),
        'add_new_item' => __('Add New Employee'),
        'edit_item' => __('Edit Employee'),
        'new_item' => __('New Employee'),
        'view_item' => __('View Employee'),
        'search_items' => __('Search Employee'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'taxonomies' => array( 'category' ),
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','thumbnail')
      ); 

    register_post_type( 'employees' , $args );
}
add_action('init', 'employees_register');

1 个答案:

答案 0 :(得分:2)

您应该使用register_taxonomy()函数注册自定义分类:

add_action( 'init', 'employee_tax' );

function create_book_tax() {
    register_taxonomy(
        'employee-categories',
        'employees',
        array(
            'label' => __( 'Employee Categories' ),
            'hierarchical' => true,
        )
    );
}

https://codex.wordpress.org/Function_Reference/register_taxonomy

然后,只有这些分类法会显示为帖子类型的选项,而不是其他帖子类型的类别。