这是我在一个项目中创建的代码。其中,您应该将您的CPT名称注册为“产品”。因此,创建的分类仅对您的CPT产品可见。在函数中添加这些代码。php文件。它很简单,不需要插件。
/* Link products CPT to categories taxonomy
*/
add_action( \'init\', \'add_category_taxonomy_to_products\' );
function add_category_taxonomy_to_products() {
register_taxonomy_for_object_type( \'category\', \'products\' ); // CPT as products
}
//Adding taxonomies for custom post type products
function create_products_taxonomies() {
$labels = array(
\'name\' => _x( \'Products Categories\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Category\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Categories\' ),
\'all_items\' => __( \'All Categories\' ),
\'parent_item\' => __( \'Parent Category\' ),
\'parent_item_colon\' => __( \'Parent Category:\' ),
\'edit_item\' => __( \'Edit Category\' ),
\'update_item\' => __( \'Update Category\' ),
\'add_new_item\' => __( \'Add New Category\' ),
\'new_item_name\' => __( \'New Category Name\' ),
\'menu_name\' => __( \'Categories\' ),
);
$args = array(
\'hierarchical\' => true, // Set this to \'false\' for non-hierarchical taxonomy (like tags)
\'labels\' => $labels,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'categories\' ),
);
register_taxonomy( \'products_categories\', array( \'products\' ), $args );
flush_rewrite_rules() ;
}
add_action( \'init\', \'create_products_taxonomies\', 0 );