2017-02-14 199 views
1

我試圖從帖子類別中爲特定帖子類型「公告」區分類別列表。如何將自定義帖子類型分類到帖子類別

我希望我公佈職位類型有其自己的類別列表,而不是與職位類別列表中混用

這裏是我的代碼

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'); 

回答

1

如果此代碼的工作,這是正規什麼鏈接類別公佈後類型:

register_taxonomy_for_object_type('category', 'announcement'); 

我們需要與您創建和註冊新的分類交流,所以與此交流一下:

register_taxonomy_for_object_type('announcement_type', 'announcement'); 

讓我知道這是否對你有用,這裏可能會有更多的問題,但這也可能就足夠了。

+0

感謝兄弟們,我只是更新了我的代碼,它現在適用於我,現在我刪除了register_taxonomy_for_object_type('category','announcement'); –

+0

很高興我能幫忙! –

1

UPDATE:

我刪除了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 
     ) 
    ); 

現在對我的作品,

如果您有這更好的答案,請分享:)我想使這段代碼更短。

相關問題