Tag and category are two default WordPress taxonomies. They help to manage and grouping your WordPress posts. However, sometimes that isn’t enough, especially when you have Custom Post Type. For example, if you have Portfolio CPT, having ‘Type’ or ‘Price Range’ taxonomy will help you grouping all the portfolio items differently from normal posts. For more information about Custom Taxonomy, WordPress Codex is one of the main resources you should refer to.
Create a Custom Taxonomy
In this example, I’m creating Type custom taxonomy and assign it to Portfolio CPT. You may copy and paste the snippet below to your functionality plugin. This custom taxonomy helps me to group or classify my portfolio based on the type of the project I’ve done.
The code might look complicated at first but it’s not. Here are some important parameters that you should know:
'name' – This is the name of you custom taxonomy. If you wish to create a custom taxonomy other than ‘Type’, simply change your taxonomy name here. Moreover, don’t forget to change the word ‘Type’ or ‘Types’ from line 8 to 22.
'show_in_nav_menus' – If you want to display your custom taxonomy, this should be set to true.
'hierarchical' – Setting this to true will make your custom taxonomy behaves like Category while setting it to false will make your custom taxonomy behaves like Tag.
register_taxonomy( 'types', array('Portfolio'), $args ); – In line 38, this is where I assigned this custom taxonomy to my Portfolio CPT. If you want to assign it to multiple post types, you may do so by using a comman. Example:
register_taxonomy( 'types', array('Post, Portfolio, Gallery'), $args );
It’s important to know that the code above does not include all parameters that can be accepted by the
register_taxonomy functions. For complete list of all parameters, please refer Parameters – register_taxonomy page on WordPress Codex.
Making Life Easier with Custom Taxonomy Generator
If you build lots of sites for clients, you’ll eventually notice that you can simply reuse the code over and over again with only some minor modifications. That’s the reason why custom taxonomy generator is very useful and becomes very handy.
One of the great tools available is WordPress Custom Taxonomy Code Generator by Themergency.
Believe it or not, the code I’ve just presented above is generated by this tool. It rocks and is very handy.
We hope this tutorial helped you to create your first taxonomy and if you’ve any question, feel free to leave your question in the comment section below.