如何给自定义文章类型(CPT)里面添加分类(Categories)?

今天尝试给自定义文章类型添加分类时发现,CPT默认的类别调用的是文章的类别,那么,有没有办法实现给自定义的文章类型添加独立的分类呢?

通过谷歌搜索,我终于找到了问题的解决方案,这个方法是老外分享的,那么下面我就把该教程的原文贴上,有时间了再翻译。

下面这个是YouTube视频的链接,国内可能播放不了

In order to start working with taxonomies, you need to register a custom post type first. Below is the reference that you can copy/paste. Make sure to tweak it according to your liking! If you need to create multiple custom post types, I highly suggest to declare a function to organize them.

register_post_type(
    'press',
    array(
        'labels' => array(
            'name' => 'Press',
            'singular_name' => 'Press',
          
        ),
        'public' => true,
        'publicly_queryable' => true,
        'has_archive' => false,
        'rewrite' => array('slug' => $data['press_slug']),
        'supports' => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes'),
        'can_export' => true,
    )
);	

Things To Know For WordPress Taxonomy

Please keep in mind that you need to change your taxonomy, please replace the “press_categories” and “press_tags” to your preferred name. The second array should also match on your custom post type so ‘press’ word should be changed. Kindly check my video for further instruction.

To add more arrays on your taxonomies, check our WordPress official documentation

WordPress Taxonomy Code For Categories

register_taxonomy('press_category', 'press', array('hierarchical' => true, 'label' => 'Categories', 'query_var' => true, 'rewrite' => true));

WordPress Taxonomy Code For Tags

register_taxonomy('press_tags', 'press', array('hierarchical' => false, 'label' => 'Tags', 'query_var' => true, 'rewrite' => true));

如果想了解更多关于此教程的内容,可以查看下这篇文章或者这篇文章。

声明:本站文章如无特别说明,均来源于网络,如有侵权,请联系邮箱[email protected]

给TA打赏
共{{data.count}}人
人已打赏
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索