将多个自定义分类附加到一个CPT?

时间:2013-09-26 作者:Austin Biggs

因此,我最近一直在开发一个复杂的CPT,出现了一些事情,让我意识到我需要在CPT上使用第二个自定义分类法。这不可能吗?此外,我目前也在使用标记,因此第二种分类法不能再细分为标记。

非常感谢您的任何建议。

1 个回复
SO网友:Howdy_McGee

首先,你使用的是什么Wordpress版本?其次,这里是一个带有2个分类法的自定义帖子类型。这有点长,因为我直接从我的一个项目中复制/粘贴它,请随意查看抄本并添加或删除您认为合适的内容。

首先,我注册post类型-following the standards.

/** Create Post Type **/
function CPT_init(){
    // Products CPT 
    register_post_type(\'product\', array(
        \'labels\'            =>  array(
            \'name\'          =>      __(\'Products\'),
            \'singular_name\' =>      __(\'Product\'),
            \'all_items\'     =>      __(\'View Products\'),
            \'add_new\'       =>      __(\'New Product\'),
            \'add_new_item\'  =>      __(\'New Product\'),
            \'edit_item\'     =>      __(\'Edit Product\'),
            \'view_item\'     =>      __(\'View Product\'),
            \'search_items\'  =>      __(\'Search Products\'),
            \'no_found\'      =>      __(\'No Products Found\'),
            \'not_found_in_trash\' => __(\'No Products in Trash\')
                                ),
        \'public\'            =>  true,
        \'publicly_queryable\'=>  true,
        \'show_ui\'           =>  true, 
        \'query_var\'         =>  true,
        \'show_in_nav_menus\' =>  false,
        \'capability_type\'   =>  \'post\',
        \'hierarchical\'      =>  false,
        \'rewrite\'           =>  array(\'with_front\' => false),
        \'menu_position\'     =>  21,
        \'supports\'          =>  array(\'title\',\'editor\', \'thumbnail\'),
        \'has_archive\'       =>  true
    )); 
}
add_action(\'init\', \'CPT_init\');
接下来,我为我的自定义帖子类型产品创建2个分类法

一个用于产品类别,另一个用于产品制造商。

/** Add Custom Taxonomy **/
function product_category_taxonomy() {
    $labels = array(
        \'name\'              => __( \'Product Categories\' ),
        \'singular_name\'     => __( \'Product Category\' ),
        \'search_items\'      => __( \'Search Product Categories\' ),
        \'all_items\'         => __( \'All Product Categories\' ),
        \'parent_item\'       => __( \'Parent Product Category\' ),
        \'parent_item_colon\' => __( \'Parent Product Category:\' ),
        \'edit_item\'         => __( \'Edit Product Category\' ), 
        \'update_item\'       => __( \'Update Product Category\' ),
        \'add_new_item\'      => __( \'Add New Product Category\' ),
        \'new_item_name\'     => __( \'New Product Category\' ),
        \'menu_name\'         => __( \'Product Categories\' ),
    ); 
    $args = array(
        \'labels\'            => $labels,
        \'public\'            =>  true,
        \'hierarchical\'      =>  true,
        \'show_in_nav_menus\' =>  false,
        \'has_archive\'       =>  true
    );
    register_taxonomy( \'protax\', \'product\', $args ); 

    $labels = array(
        \'name\'              => __( \'Manufacturers\' ),
        \'singular_name\'     => __( \'Manufacturer\' ),
        \'search_items\'      => __( \'Search Manufacturers\' ),
        \'all_items\'         => __( \'All Manufacturers\' ),
        \'parent_item\'       => __( \'Parent Manufacturer\' ),
        \'parent_item_colon\' => __( \'Parent Manufacturer:\' ),
        \'edit_item\'         => __( \'Edit Manufacturer\' ), 
        \'update_item\'       => __( \'Update Manufacturer\' ),
        \'add_new_item\'      => __( \'Add New Manufacturer\' ),
        \'new_item_name\'     => __( \'New Manufacturer\' ),
        \'menu_name\'         => __( \'Manufacturer\' ),
    );
    $args = array(
        \'labels\'            => $labels,
        \'public\'            =>  true,
        \'hierarchical\'      =>  true,
        \'show_in_nav_menus\' =>  false,
        \'has_archive\'       =>  true
    );
    register_taxonomy( \'mantax\', \'product\', $args ); 
}
add_action( \'init\', \'product_category_taxonomy\');

结束

相关推荐

Separate tags with semicolon

有没有人知道有没有办法更改类似标记的自定义分类法的回调函数,以便它在分号后而不是逗号后打断术语?我意识到我可以rewrite the whole meta box, 但如果有更简单的方法,我宁愿不要。