2015-07-12 18 views
0

我在functions.php中的代碼是:我想將類別添加到自定義文章但不起作用。爲什麼?

register_post_type('final_access_sites', array(
      'public' => true, 
      'labels' => array(
       'name' => 'Post a site', 
       'add_new' => 'Add new site', 
       'add_new_item' => 'Add new site', 
       'edit_item' => 'Update an existing site', 
       'view_item' => 'View the site', 
       'all_items' => 'All sites', 

       ), 
      'taxonomies' => array('category'), 
      'menu_position' => 30, 
      'menu_icon' => get_template_directory_uri().'/images/final_post.png', 
      'supports' => array('thumbnail','title'), 
      'order' => 'ASC', 
     )); 

添加此代碼後,我看不到類別分類在我的儀表盤任何職務。我不明白爲什麼?我已經搜查過它,但沒有找到任何理由。請幫我解決這個問題。

+0

除了在代碼中輸入代碼之外,一切都是正確的。你用init來添加這個嗎? – trex005

回答

-1

嘿,也許你可以試試這個。

它的工作原理是因爲它形成了一個實際的項目。它有兩部分: 第一:自定義帖子類型和第二:它的類別。

你只需要複製它,改變自定義後類型名稱和類別名稱

有你,我希望它可以幫助! :)

function custom_post_type_projekt() { 

    $labels = array(
     'name'    => _x('Projekt', 'Post Type General Name', 'projekt'), 
     'singular_name'  => _x('Projekt', 'Post Type Singular Name', 'projekt'), 
     'menu_name'   => __('Projekte', 'projekt'), 
     'parent_item_colon' => __('Eltern-Element:', 'projekt'), 
     'all_items'   => __('Alle Projekte', 'projekt'), 
     'view_item'   => __('Projekt ansehen', 'projekt'), 
     'add_new_item'  => __('Neues Projekt hinzufügen', 'projekt'), 
     'add_new'    => __('Neues Projekt', 'projekt'), 
     'edit_item'   => __('Projekt bearbeiten', 'projekt'), 
     'update_item'   => __('Projekt aktualisieren', 'projekt'), 
     'search_items'  => __('Projekt suchen', 'projekt'), 
     'not_found'   => __('Projekt nicht gefunden', 'projekt'), 
     'not_found_in_trash' => __('Projekt nicht im Papierkorb gefunden', 'projekt'), 
    ); 
    $args = array(
     'label'    => __('Projekte', 'projekt'), 
     'description'   => __('Hier können beliebig viele Projekte angelegt werden', 'projekt'), 
     'labels'    => $labels, 
     'supports'   => array('title', 'editor', 'thumbnail', 'trackbacks', 'custom-fields',), 
     // 'taxonomies'   => array('category'), 
     'hierarchical'  => true, 
     'public'    => true, 
     'show_ui'    => true, 
     'show_in_menu'  => true, 
     'show_in_nav_menus' => true, 
     'show_in_admin_bar' => true, 
     'menu_position'  => 5, 
     'menu_icon'   => 'dashicons-media-document', 
     'can_export'   => true, 
     'has_archive'   => true, 
     'exclude_from_search' => true, 
     'publicly_queryable' => true, 
     'rewrite'    => false, 
     'capability_type'  => 'post', 
    ); 
    register_post_type('projekt', $args); 


} 
     // Hook into the 'init' action 
      add_action('init', 'custom_post_type_projekt', 0); 

     function my_taxonomies_product() { 
     $labels = array(
     'name'    => _x('Projektkategorien', 'taxonomy general name'), 
     'singular_name'  => _x('Projektkategorien', 'taxonomy singular name'), 
     'search_items'  => __('Suche Projekt Kategorien'), 
     'all_items'   => __('Alle Projektkategorien'), 
     'parent_item'  => __('Parent Projektkategorien'), 
     'parent_item_colon' => __('Parent Projektkategorien:'), 
     'edit_item'   => __('Edit Projektkategorien'), 
     'update_item'  => __('Update Projektkategorien'), 
     'add_new_item'  => __('Neu Projektkategorie hinzufügen'), 
     'new_item_name'  => __('Neue Projekt Kategorie'), 
     'menu_name'   => __('Projektkategorien'), 
    ); 
     $args = array(
     'labels' => $labels, 
     'hierarchical' => true, 
    ); 
     register_taxonomy('projektkategorien', 'projekt', $args); 
    } 
    add_action('init', 'my_taxonomies_product', 0); 
+0

是否必須註冊自定義分類?實際上,我想使用wordpress中的內置分類的'category'。我想在自定義文章中使用這個現成的分類。有沒有辦法像上面我自己的代碼那樣做。請告訴我,在註冊我的自定義帖子的時候,這不足以支持「類別」分類標準的自定義帖子嗎? –

+0

簡單的回答:不,但我已經有同樣的想法,我嘗試了很多,但最終與上面的代碼。 – meck373

1

使用register_taxonomy_for_object_type功能註冊分類添加到掛號郵遞類型。

add_action('init','add_categories_to_cpt'); 
function add_categories_to_cpt(){ 
    register_taxonomy_for_object_type('category', 'final_access_sites'); 
} 
相關問題