In this article, we will illustrate how to create custom taxonomies in WordPress from scratch and how to display them with your current WordPress theme.
Table of Contents
Most of the people think that WordPress is all about creating pages and posts and including them under categories and tags.
But in reality, WordPress can be beyond all those ready-made things because it offers the options to create custom post types and custom taxonomies.
This feature is not available on several other platforms, and hence, most of the developers like to work with WordPress for all the different types of websites.
But first, let us understand what custom taxonomy really is.
What Is A Custom Taxonomy?
Taxonomy is a technical term, and it is funny that everyone uses taxonomy in WordPress, but they are not aware of it. Taxonomy is a method by which WordPress groups certain normal posts or custom post types together.
The two most popular taxonomies that everyone uses are Categories and Tags. Categories and Tags literally group posts together in different groups.
Similarly, you can create a custom taxonomy to group certain posts in separate groups for convenience like Locations. For example, one of the most popular custom taxonomies that most people create is Topics.
Even though there is Categories, people like to differentiate their posts based on Topics for their own convenience. Even they can show the menu on their website with the custom taxonomy that they make.
Suppose you have a website where you review books, movies and stuff like that. In the Topics, you can have the genres rather than including them under categories. It can also stand for sub-categories or sub-menus.
You should create custom taxonomies in WordPress to keep things under control before everything spreads out. You can literally sort your posts and make changes to that group of posts very easily.
How To Create Custom Taxonomies In WordPress?
There are two ways to create custom taxonomies in WordPress. The first method is by using a plugin, and it is good for those who do not like coding.
The second method is by coding, and it is for those who do not want a heavy plugin that would slow down the performance.
Method 1 – How To Create Custom Taxonomies In WordPress Using A Plugin
The plugin that you require installing to create a custom taxonomy is Simple Taxonomy. So, go to your Admin Dashboard and from the left menu, select Plugins Add New.
Search the plugin by its name and install and activate it. Once done, come back to Dashboard and go to Settings Custom Taxonomies.
You will get a new screen where you have to fill up a form. The first field is the name of the custom taxonomy you want to create. The name must be in lower character.
The second option is to select whether it is a hierarchical sub-category or not like tags. So, choose True or False according to your requirements. The third option lets you tag the custom taxonomy with a post type and likewise.
In the new screen, you have to enter important details. Assuming that you are creating the taxonomy like Topics, you have to tell WordPress about the user interface that would be associated with it.
Fill in the details accordingly for the UI and click on Add Taxonomy button. Once this is created, you can check any post, and you will see that it has it on the right side just like categories and tags.
Method 2 – How To Create Custom Taxonomies In WordPress Using Coding
Those who are interested in coding, you can create custom taxonomies in WordPress very easily. All you have to do is to go to Editor from Appearance option in the Dashboard.
Edit the file functions.php and paste the following code. It is important to note that the following code is only for those who want to create a custom taxonomy in a hierarchal structure like sub-category or sub-menu.
//hook into the init action and call create_book_taxonomies when it fires add_action( 'init', 'create_topics_hierarchical_taxonomy', 0 ); //create a custom taxonomy name it topics for your posts function create_topics_hierarchical_taxonomy() { // Add new taxonomy, make it hierarchical like categories //first do the translations part for GUI $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' =__( 'Topics' ), ); // Now register the taxonomy register_taxonomy('topics',array('post'), array( 'hierarchical' =true, 'labels' =$labels, 'show_ui' =true, 'show_admin_column' =true, 'query_var' =true, 'rewrite' =array( 'slug' = 'topic' ), )); }
If you want to create custom taxonomies in WordPress in non-hierarchical manner, use the following code in functions.php.
//hook into the init action and call create_topics_nonhierarchical_taxonomy when it fires add_action( 'init', 'create_topics_nonhierarchical_taxonomy', 0 ); function create_topics_nonhierarchical_taxonomy() { // Labels part for the GUI $labels = array( 'name' =_x( 'Topics', 'taxonomy general name' ), 'singular_name' =_x( 'Topic', 'taxonomy singular name' ), 'search_items' = __( 'Search Topics' ), 'popular_items' =__( 'Popular Topics' ), 'all_items' = __( 'All Topics' ), 'parent_item' = null, 'parent_item_colon' = null, 'edit_item' = __( 'Edit Topic' ), 'update_item' = __( 'Update Topic' ), 'add_new_item' =__( 'Add New Topic' ), 'new_item_name' =__( 'New Topic Name' ), 'separate_items_with_commas' = __( 'Separate topics with commas' ), 'add_or_remove_items' = __( 'Add or remove topics' ), 'choose_from_most_used' =__( 'Choose from the most used topics' ), 'menu_name' = __( 'Topics' ), ); // Now register the non-hierarchical taxonomy like tag register_taxonomy('topics','post',array( 'hierarchical' = false, 'labels' =$labels, 'show_ui' = true, 'show_admin_column' = true, 'update_count_callback' = '_update_post_term_count', 'query_var' = true, 'rewrite' = array( 'slug' = 'topic' ), )); }
Displaying Custom Taxonomies
When you create custom taxonomies in WordPress using code, you have to write a separate line of code to show it.
All you have to do is to open single.php from the Editor and paste the following line of code. Remember to paste the code within the loop present in the file.
the_terms( $post-ID, 'topics', 'Topics: ', ', ', ' ' );
That is it, and you just created a custom taxonomy and made your life easy on WordPress.