类别分开的自定义帖子类型

时间:2020-11-25 作者:Nicola

我创建了两个自定义帖子类型(movie type1, movie 2) 通过functions.php, 但在创建新类别时,该类别会在帖子中重复自身(Article) 和其他CPT(movie type1, movie 2) 为什么?

enter image description here

function custom_post_type_week() {
     
    // Set UI labels for Custom Post Type
        $labels = array(
            \'name\'                => _x( \'Movie type 1\', \'Post Type General Name\', \'twentythirteen\' ),
            \'singular_name\'       => _x( \'Movie type 1\', \'Post Type Singular Name\', \'twentythirteen\' ),
            \'menu_name\'           => __( \'movie type 1\', \'twentythirteen\' ),
            \'parent_item_colon\'   => __( \'Parent Movie\', \'twentythirteen\' ),
            \'all_items\'           => __( \'All Movies\', \'twentythirteen\' ),
            \'view_item\'           => __( \'View Movie\', \'twentythirteen\' ),
            \'add_new_item\'        => __( \'Add New Movie\', \'twentythirteen\' ),
            \'add_new\'             => __( \'Add New\', \'twentythirteen\' ),
            \'edit_item\'           => __( \'Edit Movie\', \'twentythirteen\' ),
            \'update_item\'         => __( \'Update Movie\', \'twentythirteen\' ),
            \'search_items\'        => __( \'Search Movie\', \'twentythirteen\' ),
            \'not_found\'           => __( \'Not Found\', \'twentythirteen\' ),
            \'not_found_in_trash\'  => __( \'Not found in Trash\', \'twentythirteen\' ),
        );
     

    $args = array(
        \'label\'               => __( \'movies\', \'twentythirteen\' ),
        \'description\'         => __( \'Movie news and reviews\', \'twentythirteen\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', ),
        \'hierarchical\'        => true,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 5,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\',
        \'show_in_rest\'        => true,
         
        // This is where we add taxonomies to our CPT
        \'taxonomies\'          => array( \'category\',\'post_tag\' ),
    );
     
  
    register_post_type( \'movies\', $args );
 
}
 
 
add_action( \'init\', \'custom_post_type_week\', 0 );

1 个回复
最合适的回答,由SO网友:Awais 整理而成

Categorypost_tag 是默认的WP分类法,因为您在CPT函数中附加了它们,所以它们会出现在CPT下以及Posts菜单下

您需要做的是创建一个自定义的分类法movie_cat 并附加到您的定制CPT上,例如。movies

// Register Custom Taxonomy
function custom_taxonomy() {

    $labels = array(
        \'name\'                       => _x( \'Movie Categories\', \'Taxonomy General Name\', \'twentythirteen\' ),
        \'singular_name\'              => _x( \'Movie Category\', \'Taxonomy Singular Name\', \'twentythirteen\' ),
        \'menu_name\'                  => __( \'Movie Category\', \'twentythirteen\' ),
        \'all_items\'                  => __( \'All Items\', \'twentythirteen\' ),
        \'parent_item\'                => __( \'Parent Item\', \'twentythirteen\' ),
        \'parent_item_colon\'          => __( \'Parent Item:\', \'twentythirteen\' ),
        \'new_item_name\'              => __( \'New Item Name\', \'twentythirteen\' ),
        \'add_new_item\'               => __( \'Add New Item\', \'twentythirteen\' ),
        \'edit_item\'                  => __( \'Edit Item\', \'twentythirteen\' ),
        \'update_item\'                => __( \'Update Item\', \'twentythirteen\' ),
        \'view_item\'                  => __( \'View Item\', \'twentythirteen\' ),
        \'separate_items_with_commas\' => __( \'Separate items with commas\', \'twentythirteen\' ),
        \'add_or_remove_items\'        => __( \'Add or remove items\', \'twentythirteen\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used\', \'twentythirteen\' ),
        \'popular_items\'              => __( \'Popular Items\', \'twentythirteen\' ),
        \'search_items\'               => __( \'Search Items\', \'twentythirteen\' ),
        \'not_found\'                  => __( \'Not Found\', \'twentythirteen\' ),
        \'no_terms\'                   => __( \'No items\', \'twentythirteen\' ),
        \'items_list\'                 => __( \'Items list\', \'twentythirteen\' ),
        \'items_list_navigation\'      => __( \'Items list navigation\', \'twentythirteen\' ),
    );
    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => true,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => true,
    );
    register_taxonomy( \'movie_cat\', array( \'movies\' ), $args );

}
add_action( \'init\', \'custom_taxonomy\', 0 );
以及在CPT函数中附加自定义分类法的部分,请使用以下代码附加movie_cat 分类学

// This is where we add taxonomies to our CPT
    \'taxonomies\'          => array( \'movie_cat\' ), 

相关推荐