Skip to main content

How To Create Categories For Custom Post type

Submitted by admin on Wed, 12/08/2021 - 13:20
/wordpress-custom-post-categorie
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.


// 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.

Tutorial Category