我正在尝试将类别添加到自定义帖子但不起作用。为什么?

时间:2015-07-12 08:23:14

标签: wordpress categories custom-post-type taxonomy

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

添加此代码后,我在任何帖子的仪表板中都没有看到类别分类。我不明白为什么?我搜索过它,但没有找到任何理由。请帮我解决这个问题。

2 个答案:

答案 0 :(得分: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');
}

答案 1 :(得分:-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 );