How To Create Categories For Custom Post type

wordpress-custom-post-categories
I have already explained how to create custom post / content type in WordPress. In this tutorial, we will learn how to create categories for our custom post type.

Let’s say, we have created a new content type book. And now we want to create Subject categories for book post type. register_taxonomy() is used to register new category.

Add following codes in functions.php of your theme.

1
2
3
4
5
6
7
8
9
10
11
12
13
// register subjects for book
function book_taxonomy() {
  register_taxonomy(
    'subject',
    'book', // This is post type.
    array(
      'label' => __( 'Subject' ),
      'rewrite' => array( 'slug' => 'subject', 'hierarchical' => true ),
      'hierarchical' => true,
    )
  );
}
add_action( 'init', 'book_taxonomy' );

You should now get option to create Subjects for Book post type.
wordpress-custom-categories


Hope this tutorial was helpful.
If you need help with categories for custom post type in WordPress, Please leave a comment below and I will get back to you.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *