我学会了通过functions.php
在数组中,如下所示:
add_action(\'init\', \'all_custom_post_types\');
function all_custom_post_types() {
$types = array(
array(\'the_type\' => \'event\',
\'single\' => \'Event\',
\'plural\' => \'Events\'
),
array(\'the_type\' => \'resource\',
\'single\' => \'Resource\',
\'plural\' => \'Resources\'
),
array(\'the_type\' => \'team\',
\'single\' => \'Team\',
\'plural\' => \'Team\'
),
);
这些帖子类型的选项可以声明如下:
foreach ($types as $type) {
$the_type = $type[\'the_type\'];
$single = $type[\'single\'];
$plural = $type[\'plural\'];
$labels = array(
\'name\' => _x($plural, \'post type general name\'),
\'singular_name\' => _x($single, \'post type singular name\'),
\'add_new\' => _x(\'Add New\', $single),
\'add_new_item\' => __(\'Add New \'. $single),
\'edit_item\' => __(\'Edit \'.$single),
\'new_item\' => __(\'New \'.$single),
\'view_item\' => __(\'View \'.$single),
\'search_items\' => __(\'Search \'.$plural),
\'not_found\' => __(\'No \'.$plural.\' found\'),
\'not_found_in_trash\' => __(\'No \'.$plural.\' found in Trash\'),
\'parent_item_colon\' => \'\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array(
\'with_front\' => false
),
\'capability_type\' => \'post\',
\'hierarchical\' => true,
\'has_archive\' => true,
\'menu_position\' => 5,
\'supports\' => array(\'title\',\'editor\',\'author\'),
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'taxonomies\' => array( \'category\', \'post_tag\' ),
);
register_post_type($the_type, $args);
}
}
但是如果我想要
taxonomies => array( \'category\', \'post_tag\' ),
要应用于的行
resource
仅post类型?在本例中,我希望启用分类法,如下所示:
resource
: 类别和标签event
: 仅标记team
: 没有分类学术语可以像我声明自定义帖子类型那样实现,还是应该单独声明它们以实现更细粒度的控制?
最合适的回答,由SO网友:user7459842 整理而成
我已经修改了你的代码,应该可以用了。。请试一试
add_action(\'init\', \'all_custom_post_types\');
function all_custom_post_types() {
$types = array(
array(\'the_type\' => \'event\',
\'single\' => \'Event\',
\'plural\' => \'Events\'
),
array(\'the_type\' => \'resource\',
\'single\' => \'Resource\',
\'plural\' => \'Resources\'
),
array(\'the_type\' => \'team\',
\'single\' => \'Team\',
\'plural\' => \'Team\'
),
);
foreach ($types as $type) {
$the_type = $type[\'the_type\'];
$single = $type[\'single\'];
$plural = $type[\'plural\'];
$labels = array(
\'name\' => _x($plural, \'post type general name\'),
\'singular_name\' => _x($single, \'post type singular name\'),
\'add_new\' => _x(\'Add New\', $single),
\'add_new_item\' => __(\'Add New \'. $single),
\'edit_item\' => __(\'Edit \'.$single),
\'new_item\' => __(\'New \'.$single),
\'view_item\' => __(\'View \'.$single),
\'search_items\' => __(\'Search \'.$plural),
\'not_found\' => __(\'No \'.$plural.\' found\'),
\'not_found_in_trash\' => __(\'No \'.$plural.\' found in Trash\'),
\'parent_item_colon\' => \'\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array(
\'with_front\' => false
),
\'capability_type\' => \'post\',
\'hierarchical\' => true,
\'has_archive\' => true,
\'menu_position\' => 5,
\'supports\' => array(\'title\',\'editor\',\'author\'),
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
);
if($the_type==\'resource\'){
$args[\'taxonomies\'] = array( \'category\', \'post_tag\' );
}elseif($the_type=="event"){
$args[\'taxonomies\'] = array( \'post_tag\' );
}else{
}
register_post_type($the_type, $args);
}
}