如何将自定义帖子类型类别与帖子类别分开

时间:2017-02-14 18:15:13

标签: php wordpress

我试图将特定帖子类型"公告"的类别列表分开。来自帖子类别。

我希望我的公告帖子类型有自己的类别列表,而不是与帖子类别列表混合

这是我的代码

function announcement_post_types() {
    register_post_type(
        'announcement',
        array(
            'labels' => array(
                'name' => __( 'Announcements' ),
                'singular_name' => __( 'announcement' ),
                'menu_name' => __( 'Announcements' ),
                'all_items' => __( 'All Announcements' ),
                'view_item' => __( 'View Announcement' ),
                'add_new_item' => __( 'New Announcement' ),
                'add_new' => __( 'New Announcement' ),
                'edit_item' => __( 'Edit Announcements' ),
                'update_item' => __( 'Update' ),
                'search_items' => __( 'Search' ),
                'not_found' => __( 'Not Found' ),
                'not_found_in_trash' => __( 'Not Found in Trash' ),
            ),
            'public' => true,
            'has_archive' => false,
            'rewrite' => array('slug' => 'announcement'),
            'menu_icon' => 'dashicons-admin-page',
            'supports' => array('title', 'editor', 'custom-fields')
        )
    );
    register_taxonomy(  
    'announcement_type',  
    'announcement',  // this is the custom post type(s) I want to use this taxonomy for
        array(  
            'hierarchical' => false,  
            'label' => 'News Types',  
            'query_var' => true,  
            'rewrite' => true  
        )  
    );  
    register_taxonomy_for_object_type('category', 'announcement');
}
add_action('init', 'announcement_post_types');

2 个答案:

答案 0 :(得分:1)

如果此代码有效,则将常规类别链接到公告帖子类型:

register_taxonomy_for_object_type('category', 'announcement');

我们需要将您与您创建和注册的新分类法进行交换,因此请与此交换:

register_taxonomy_for_object_type('announcement_type', 'announcement');

让我知道如果这对您有所帮助,可能会有更多问题,但这也可能就足够了。

答案 1 :(得分:1)

更新:

我删除了register_taxonomy_for_object_type('category', 'announcement');并更改为此

$labels = array(
        'name' => _x( 'Categories', 'taxonomy general name' ),
        'singular_name' =>_x( 'Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Categories' ),
        'popular_items' => __( 'Popular Categories' ),
        'all_items' => __( 'All Categories' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Announcement Category' ),
        'update_item' => __( 'Update Announcement Category' ),
        'add_new_item' => __( 'Add New Announcement Category' ),
        'new_item_name' => __( 'New Announcement Category' ),
        'separate_items_with_commas' => __( 'Separate categories with commas' ),
        'add_or_remove_items' => __( 'Add or remove Announcement categories' ),
        'choose_from_most_used' => __( 'Choose from the most used categories' )
        );
    register_taxonomy(  
    'announcement_type',  
    'announcement',  // this is the custom post type(s) I want to use this taxonomy for
        array(  
            'hierarchical' => false,  
            'label' => 'News Types',  
            'query_var' => true,  
            'label' => __('Announcement Category'),
            'labels' => $labels,
            'hierarchical' => true,
            'show_ui' => true,
            'rewrite' => true  
        )  
    );  

现在对我有用,

如果你有更好的答案请分享:)我想让这段代码更短。