将类别选择添加到自定义帖子类型

时间:2016-12-07 04:57:04

标签: php css wordpress function woocommerce

我希望能够将我的WooCommerce产品发布到我的“帖子”类别中。基于下面这个鳕鱼,它是可能的。这是我在functions.php中使用的代码。当我在Woo中制作新产品时,这些类别是可点击的,但是它不会发布到类别本身。欣赏有关此事的任何见解。

将类别选择添加到自定义帖子类型

function reg_cat() {
     register_taxonomy_for_object_type('category','CUSTOM_POST_TYPE');
}
add_action('init', 'reg_cat');

2 个答案:

答案 0 :(得分:0)

请尝试使用此代码注册新版权

function custom_taxonomy() {  
    register_taxonomy(  
        'custom_categories',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces). 
        'post_type',        //post type name
        array(  
            'hierarchical' => true,  
            'label' => 'Themes store',  //Display name
            'query_var' => true,
            'rewrite' => array(
                'slug' => 'themes', // This controls the base slug that will display before each term
                'with_front' => false // Don't display the category base before 
            )
        )  
    );  
}  
add_action( 'init', 'custom_taxonomy');

注意:请在主题function.php或您的插件中添加此代码

=============================================== ========================

function reg_cat() {
         register_taxonomy_for_object_type('category','CUSTOM_POST_TYPE');
    }
add_action('init', 'reg_cat');

此代码工作类别显示在自定义帖子类型的术语部分中请参阅

enter image description here enter image description here

答案 1 :(得分:0)

对于注册自定义分类,您可以尝试以下代码,以获得更多https://codex.wordpress.org/Function_Reference/register_taxonomy

<?php
add_action( 'init', 'create_book_tax' );

function create_book_tax() {
    register_taxonomy(
        'genre',
        'book',
        array(
            'label' => __( 'Genre' ),
            'rewrite' => array( 'slug' => 'genre' ),
            'hierarchical' => true,
        )
    );
}
?>