$args = array(
'label' => __( 'Eventr', 'dnp_theme' ),
'description' => __( 'What clients say ', 'dnp_theme' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail'),
'taxonomies' => array( 'Eventr', 'product_cat'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'show_in_admin_bar' => true,
'menu_position' => 10,
'menu_icon' => 'dashicons-images-alt2',
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type( 'Eventr', $args );
这是我的代码,用于在我的自定义文章类型中获取WooCommerce产品类别。
在分类法中,我添加了woocommerce产品类别分类法“product_cat”,但它没有显示在管理面板上。
帮助我了解如何在自定义帖子类型管理菜单中添加WooCommerce产品类别。
我也有同样的问题,我发现后来注册分类学似乎是有效的。因此,在注册自定义文章类型后的函数中,添加这样的内容,其中custom_post_type
键是您注册的键:
add_action( 'init', 'add_product_cat_to_custom_post_type' );
function add_product_cat_to_custom_post_type() {
register_taxonomy_for_object_type( 'product_cat', 'custom_post_type' );
}
对于2020年有这个问题的人和古腾堡编辑:
步骤1:使用Register\u taxonomy\u for_object\u type
或使用taxonomies
字段在post type注册参数中设置分类(如其他答案和问题中所示)。
步骤2:将分类法包括在RESTAPI中,并通过将show_in_REST
设置为true
使其在块编辑器中可用。对于product\u cat
,这意味着使用woocommerce\u分类法\u args\u product\u cat
过滤器更改此设置。
使用在init
上调用的函数register\u taxonomy\u for_object\u type()
,但请确保时间正确,以便在CPT注册后触发此调用。请注意,在本例中使用99作为优先级设置。
// attach product category taxonomy to "service" post type
add_action( 'init', function() {
register_taxonomy_for_object_type( 'product_cat', 'service' );
}, 10, 99);