简体   繁体   中英

Restrict category for custom post type in wordpress

I have created a custom post type for adding employees. I have added the category taxonomy for it. Then I have added a category named "Employees" (slug: employees). But when I add a new employee, it shows me all the categories to choose. How can I restrict it to show only "Employees" category here?

Here is my php code in functions.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');

You should register custom taxonomies with the register_taxonomy() function:

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

Then, only these taxonomies will show as options for the post type, rather than Categories of other post types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM