如何在自定义帖子类型中添加WooCommerce产品类别

时间:2015-10-09 12:12:37

标签: php wordpress woocommerce

$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产品类别。

在Taxonomies中,我添加了woocommerce产品类别分类“product_cat”,但不会在管理面板上显示。

帮助我了解如何在自定义帖子类型管理员菜单中添加WooCommerce产品类别。

3 个答案:

答案 0 :(得分:4)

我遇到了同样的问题,我发现之后注册分类似乎有效。因此,在注册自定义帖子类型后,在您的函数中添加类似这样的内容,其中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' ); }

答案 1 :(得分:0)

使用在register_taxonomy_for_object_type()上调用的函数init,但是请确保时间正确,以便在您注册CPT后触发该调用。请注意,在这种情况下,使用99作为优先级设置。

// attach product category taxonomy to "service" post type
add_action( 'init', function() {
    register_taxonomy_for_object_type( 'product_cat', 'service' );
}, 10, 99);

答案 2 :(得分:0)

对于在2020年有此问题并与Gutenberg编辑一起的任何人:

步骤1: 使用register_taxonomy_for_object_type注册分类法,或者使用taxonomies字段在帖子类型注册参数中设置分类法(如其他答案和问题一样)。

步骤2: 通过将show_in_rest设置为true,可以将分类法包含在REST API中,并使其在块编辑器中可用。 对于product_cat,这意味着使用woocommerce_taxonomy_args_product_cat过滤器更改此设置。

相关问题