簡體   English   中英

限制Wordpress中自定義帖子類型的類別

[英]Restrict category for custom post type in wordpress

我創建了一個自定義帖子類型來添加員工。 我已經為其添加了類別分類法。 然后,我添加了一個名為“雇員”的類別(子句:雇員)。 但是,當我添加新員工時,它會顯示所有要選擇的類別。 如何限制它在此處僅顯示“雇員”類別?

這是我在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');

您應該使用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

然后,只有這些分類法將顯示為帖子類型的選項,而不是其他帖子類型的類別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM