Php 如何将自定义帖子类型类别与帖子类别分开
我试图将特定帖子类型“公告”的类别列表与帖子类别分开 我希望我的公告文章类型有自己的类别列表,而不是与文章类别列表混合 这是我的密码Php 如何将自定义帖子类型类别与帖子类别分开,php,wordpress,Php,Wordpress,我试图将特定帖子类型“公告”的类别列表与帖子类别分开 我希望我的公告文章类型有自己的类别列表,而不是与文章类别列表混合 这是我的密码 function announcement_post_types() { register_post_type( 'announcement', array( 'labels' => array( 'name' => __( 'Announcements'
function announcement_post_types() {
register_post_type(
'announcement',
array(
'labels' => array(
'name' => __( 'Announcements' ),
'singular_name' => __( 'announcement' ),
'menu_name' => __( 'Announcements' ),
'all_items' => __( 'All Announcements' ),
'view_item' => __( 'View Announcement' ),
'add_new_item' => __( 'New Announcement' ),
'add_new' => __( 'New Announcement' ),
'edit_item' => __( 'Edit Announcements' ),
'update_item' => __( 'Update' ),
'search_items' => __( 'Search' ),
'not_found' => __( 'Not Found' ),
'not_found_in_trash' => __( 'Not Found in Trash' ),
),
'public' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'announcement'),
'menu_icon' => 'dashicons-admin-page',
'supports' => array('title', 'editor', 'custom-fields')
)
);
register_taxonomy(
'announcement_type',
'announcement', // this is the custom post type(s) I want to use this taxonomy for
array(
'hierarchical' => false,
'label' => 'News Types',
'query_var' => true,
'rewrite' => true
)
);
register_taxonomy_for_object_type('category', 'announcement');
}
add_action('init', 'announcement_post_types');
如果此代码正常工作,则将常规类别链接到公告栏类型:
register_taxonomy_for_object_type('category', 'announcement');
我们需要将其与您创建和注册的新分类法进行交换,因此请将其与以下内容进行交换:
register_taxonomy_for_object_type('announcement_type', 'announcement');
让我知道,如果这对您有帮助,这里可能会有更多问题,但这也可能足够了。更新:
我删除了对象类型('category','announcement')的register\u分类法代码>并更改为此
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' =>_x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'popular_items' => __( 'Popular Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Announcement Category' ),
'update_item' => __( 'Update Announcement Category' ),
'add_new_item' => __( 'Add New Announcement Category' ),
'new_item_name' => __( 'New Announcement Category' ),
'separate_items_with_commas' => __( 'Separate categories with commas' ),
'add_or_remove_items' => __( 'Add or remove Announcement categories' ),
'choose_from_most_used' => __( 'Choose from the most used categories' )
);
register_taxonomy(
'announcement_type',
'announcement', // this is the custom post type(s) I want to use this taxonomy for
array(
'hierarchical' => false,
'label' => 'News Types',
'query_var' => true,
'label' => __('Announcement Category'),
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'rewrite' => true
)
);
它现在对我有效
如果你对此有更好的答案,请分享:)我想把这段代码缩短。谢谢你,伙计,我刚刚更新了我的代码,现在它对我有效了。我删除了对象类型(“类别”、“公告”)的注册分类法;很高兴我能帮忙@约瑟夫拉里奥萨-我不确定删除这个是否可以。引用文档:在为自定义帖子类型注册自定义分类法时,最好是安全的。在函数后面使用register\u taxonomy\u for\u object\u type()将它们互连。否则,您可能会遇到陷阱,在解析请求或预获取帖子期间运行的筛选器回调中,帖子类型没有附加到该回调中。
-我不确定是否可以删除该帖子。引用文档:在为自定义帖子类型注册自定义分类法时,最好是安全的。在函数后面使用register\u taxonomy\u for\u object\u type()将它们互连。否则,您可能会遇到陷阱,在解析请求或预获取帖子期间运行的过滤器回调中没有附加帖子类型。
-