php - 如何将自定义帖子类型类别分开以发布类别

标签 php wordpress

我试图将特定帖子类型“公告”的类别列表与帖子类别分开。

我希望我的公告帖子类型有自己的类别列表,而不是与帖子类别列表混合

这是我的代码

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');

如果这对您有用,请告诉我,这里可能还有更多问题,但这可能也足够了。

关于php - 如何将自定义帖子类型类别分开以发布类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42233405/

相关文章:

php - Doctrine 不允许 ResultSetMappingBuilder 工作

php - 追加相关的子选择选项一次

wordpress - 允许 "customer"用户角色在前端使用acf_form上传文件

php - Faker 生成奇怪的电话号码?

php - 是否可以更改 echo 输出的位置?

php - Zend 框架使用数据表

css - 以升级友好的方式将 CSS 样式表添加到 Wordpress RSS 提要

css - 如何找到放置@import字体的文件

javascript - Wordpress 媒体库附件详细信息

php - WordPress 中使用 eval() 插件的漏洞示例