Custom taxonomies can really help a lot when you want to categorize the content in a better way using the wordpress. The wordpress codex has got a guide on how to register the custom taxonomies but that is not the detailed one, so this article has a detailed guide on the wordpress custom taxonomy and procedure of registering one using the wordpress default functions.php file.
What is: wordpress custom taxonomy:
Custom taxonomies are actually like the wordpress categories which are for the default posts. So the default categories are like taxonomies needed to categorize the content which you publish. But suppose if you need other sort of categories like “Music” and then the subcategories like “genre” then surely the categories are not going to help you. Thus, in this case you will register a new taxonomy type for wordpress for using it under the posts section named as music and now you will be able to allot two categories to your posts, first one the default categories and second one the music.
Thus, in the end you will be able to get two taxonomies type for your default wordpress posts, you can also register custom taxonomies for the custom post types. Only you will need to provide the unique custom post id for registering the custom taxonomy just for that specific custom post type.
Registering wordpress custom taxonomy using functions.php:
You are going to need a good code editor for editing the functions.php of your current wordpress theme. Connect to your host with the ftp access and go to wp-content/themes/your-current-theme and edit the functions.php, add the following function in your functions.php:
add_action( 'init', 'create_topic_taxonomies', 0 ); // create two taxonomies, genres and writers for the post type "book" function create_topic_taxonomies() { // Add new taxonomy, make it hierarchical (like categories) $labels = array( 'name' => _x( 'Topics', 'taxonomy general name' ), 'singular_name' => _x( 'Topic', 'taxonomy singular name' ), 'search_items' => __( 'Search Topics' ), 'all_items' => __( 'All Topics' ), 'parent_item' => __( 'Parent Topic' ), 'parent_item_colon' => __( 'Parent Topic:' ), 'edit_item' => __( 'Edit Topic' ), 'update_item' => __( 'Update Topic' ), 'add_new_item' => __( 'Add New Topic' ), 'new_item_name' => __( 'New Topic Name' ), 'menu_name' => __( 'Topic' ), ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'topic' ), ); register_taxonomy( 'topic', array( 'post' ), $args ); }
After pasting the above code just save and upload the file: ‘functions.php‘ and you are done! Using the above code we have registered the custom taxonomy named as “topic” and in the same way you can choose to create taxonomy named as “music“.
Explanation of the wordpress custom taxonomy registration code:
In the above code on can specify the label as well as visual type for the taxonomies to use them in the posts or custom post types. The labels section is in the array, and the “singular_name” specifies the single name of the category or taxonomy. Similarly the “add_new_item” is for the adding the new category under the custom taxonomy type which you have just registered for the wordpress posts. So, in this way you can easily change the labels to appear for the custom taxonomy easily. Below is the screen shot of the custom registered taxonomy under wordpress dashboard named as topic in my case, but as I said you can easily change it into the music too!
The show admin column is going to show the categories or the custom taxonomy into the wordpress posts column, just when we list all the posts in the dashboard.
What is: slug in the code:
Slug is really important for registering the custom taxonomy as it gives you the seo friendly url, you might have noticed that the default categories have the word category in the url, thus on changing the slug as desired in the code you can change the url of the custom taxonomy, like instead of category the url will have your desired slug name. In the above code the slug name is selected to be topic like this: ‘slug’ => ‘topic’ thus, its damn easy for you to change the category url as desired in the wordpress by adding a new sort of custom taxonomy.
Easily convert the custom taxonomy to tag like interface in wordpress dashboard:
A category selection menu is there for your to add categories to your posts, so same is the case with the newly registered wordpress custom taxonomy. It will have a selection interface for you to add or remove certain categories in the custom taxonomy to use with that specific post.
The newly registerd wordpress custom taxonomy will have the option of adding a sub category also or I may call sub taxonomy, don’t get confused with the same words like taxonomy or category since these mean the same! But if you need the tag like interface for the custom taxonomy then this is also possible, in this case the tags will be independent and there will be no option of adding the sub category or tag, since you must know that the tags behave independently. To convert the custom taxonomies have the tag like interface you need to convert: ‘hierarchical’ => true, to the ‘hierarchical’ => false, and after making it false go to the dashboard and open up the posts there you will be able to see another tag field added in the sidebar.
Registering wordpress custom taxonomy for custom post type:
You can easily register the wordpress custom taxonomy for the custom post type just, like when you will register the custom post type for the wordpress you will not have a separate category section other than that of default posts. So, in this case you will register a new custom taxonomy just using the above provided code and you can change the word topic with any of the desired category name and the slug.
The whole code is going to be same but only change is that you will need to change the post to the unique name of your custom post type. Like, if you have registered the wordpress custom post type using the article posted here, you must have provided the unique name of the custom post type. thus, that unique name will be used in this case, just replace post with that unique name.
Change register_taxonomy( ‘topic’, array( ‘post’ ), $args ); to the register_taxonomy( ‘topic’, array( ‘your-custom-post-name’ ), $args ); and the custom taxonomy will start to show in the custom post type of the wordpress. Now, again if you want to change anything then refer to the above guide since all the things are same. Thus, you can register as many taxonomies as you want to better manage your content.