首先,你使用的是什么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\');