In this article, you’ll learn how to add unique categories to your Custom Post Type using the register taxonomy method.
The default posts menu and its content on the left navigation menu are called the Custom Post, if you want to add or different type of post that is separate from your post, then you need to create a Custom Post Type.
In the custom post type, we have an option to add tags, and we also have an option to add a Category. If you don’t want to use this feature, you can always exclude it or you can use what’s already existing in your current default posts categories.
Before we get into the Business of the day, you need to have a Child Theme installed, for this tutorial, I’m going to be using Divi Child Theme.
Creating a Custom Post Type
If you wish to create a Custom post type, you need to however on Appearance from. The left navigation menu and then click on Theme Editor.
Once the Theme Editor is open, locate your functions.php file by scrolling through the right navigation options, you should then paste in the code below:
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, ) );
When the code above is live on your site, it will create a custom post type labeled Press. Do note that you can change it to whatever name you want your Custom Post Type to bear and you can create as many as possible by simply duplicating the code above.
You can also add as many labels as you wish, refer to the WordPress Documentation for further Intel and how it all works.
I have an in-depth article on How to Create Custom Post Types specifically, make sure to read it and gain the knowledge.
How to Inherit Categories from Post
We will register unique categories for that specific custom post type that we’ve created ‘press’.
To do that, we’ll be utilizing the Register Category Method, you can do so by adding these codes to your Child Theme’s functions.php file:
register_taxonomy('press_category', 'press', array('hierarchical' => true, 'label' => 'Categories', 'query_var' => true, 'rewrite' => true)); register_taxonomy('press_tags', 'press', array('hierarchical' => false, 'label' => 'Tags', 'query_var' => true, 'rewrite' => true));
Now, you can use whatever naming convention you manage to come up with for your taxonomy, it could be something like press_cat or presscat.
Remember that if you want to change your custom post type name, you have to do so in all the lines of code listed in this article.
You can simply paste the codes to your functions.php file, then use CTRL + F to find press, and then replace it with your CPT Name.
Once you refresh your website and then hover on the Custom Post Type on the Left Navigation Menu, you should see the Tags and Categories option.
How to Add Categories to Custom Post Type
In the Categories Sub-menu under your Custom Post Type, you can always create your custom post type’s unique categories. This applies to every single custom post type you create using this tutorial.
If you don’t want to host multiple custom categories on your website, you can always use the code below (remember to remove the Register Category Codes):
'taxonomies' => array('category', 'post_tag'),
The code displayed above will enable your Custom Post Type to inherit the current categories of your default posts.
With this tutorial, you should be able to add categories or categories to your Custom Post Type(s) easily as I’ve provided all the necessary codes you need.
If you have a lot of Custom Post Types on your WordPress Website, then you should consider bringing them together under 1 function, so you can easily falk them when you want to edit, remove or customize further.
You’d probably be interested in these resources:
- How to Create Custom Post Types in WordPress (Without Plugins).
- The Best Custom Post Type Plugins for WordPress.
Have any questions about Adding a Category to a Custom Post Type? Feel free to ask me in the comments and I’ll get back to you.