我能够创建多个自定义帖子类型,但在注册分类法时遇到了问题。我想创建每个CPT都有自己的分类法,而不是共享的。
使现代化
大家好,谢谢你们回答我的问题。我已经这样做了:
function CPT-1(){
$labels = array(
\'name\' => __( \'Careers\' ),
\'singular_name\' => __( \'Career\'),
\'menu_name\' => __( \'Careers\'),
\'parent_item_colon\' => __( \'Parent Careers\'),
..etc..
);
$args = array(
\'label\' => __( \'career\'),
\'description\' => __( \'careers\'),
....etc...
);
register_post_type( \'cpt1\', $args );
}
function CPT2(){
$labels = array(
\'name\' => __( \'Careers\' ),
\'singular_name\' => __( \'Career\'),
\'menu_name\' => __( \'Careers\'),
...etc...
);
$args = array(
\'label\' => __( \'career\'),
\'description\' => __( \'careers\'),
\'labels\' => $labels,
\'supports\' => array( \'title\', \'editor\', \'excerpt\', \'author\',
.....etc.....
);
register_post_type( \'cpt2\', $args );
}
function cma_register_taxonomies() {
$taxonomies = array(
array(
\'slug\' => \'department\',
\'single_name\' => \'Department\',
\'plural_name\' => \'Department\',
\'post_type\' => \'cpt1\',
\'rewrite\' => array( \'slug\' => \'department\' ),
),
array(
\'slug\' => \'job-type\',
\'single_name\' => \'Type\',
\'plural_name\' => \'Types\',
\'post_type\' => \'cp2\',
\'hierarchical\' => false,
)
),
);
foreach( $taxonomies as $taxonomy ) {
$labels = array(
\'name\' => $taxonomy[\'plural_name\'],
\'singular_name\' => $taxonomy[\'single_name\'],
\'search_items\' => \'Search \' . $taxonomy[\'plural_name\'],
\'all_items\' => \'All \' . $taxonomy[\'plural_name\'],
\'parent_item\' => \'Parent \' . $taxonomy[\'single_name\'],
\'parent_item_colon\' => \'Parent \' . $taxonomy[\'single_name\'] . \':\',
\'edit_item\' => \'Edit \' . $taxonomy[\'single_name\'],
\'update_item\' => \'Update \' . $taxonomy[\'single_name\'],
\'add_new_item\' => \'Add New \' . $taxonomy[\'single_name\'],
\'new_item_name\' => \'New \' . $taxonomy[\'single_name\'] . \' Name\',
\'menu_name\' => $taxonomy[\'plural_name\']
);
$rewrite = isset( $taxonomy[\'rewrite\'] ) ? $taxonomy[\'rewrite\'] : array( \'slug\' => $taxonomy[\'slug\'] );
$hierarchical = isset( $taxonomy[\'hierarchical\'] ) ? $taxonomy[\'hierarchical\'] : true;
register_taxonomy( $taxonomy[\'slug\'], $taxonomy[\'post_type\'], array(
\'hierarchical\' => $hierarchical,
\'labels\' => $labels,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => $rewrite,
));
}
}