Understand that by default, WordPress usually allows users to organize their content with tags and categories. However, custom taxonomies enable you to further customize how you want to sort the content. From this guide, you will learn the creation of custom taxonomies and all there is to know about them.
What is WordPress Taxonomy?
WordPress taxonomy refers to a specific way of organizing custom post types and groups of posts. Taxonomy is borrowed from the biological field in a classification technique known as Linnaean taxonomy. Upon purchasing or opening a WordPress site, it comes with taxonomies known as tags and categories. These are very helpful in organizing your blog posts. However, not all posts will look appealing from using only these two taxonomies.
Custom post types require more than just the tags and categories taxonomies to stand out. For example, one can decide to create a specific post named ‘Books‘ and sort it with a specific taxonomy known as ‘topics.’ From there, they can add topic terms on their topics such as Horror, Romance, and Adventure. That means the readers can easily sort the books based on the topics provided.
At times, taxonomy is hierarchical, meaning it contains a central topic like Nonfiction or Fiction. Then under every category, there would be subtopics. For example, working on a Fiction book would include subtopics such as Horror, Adventure, and Romance.
How to Display Custom Taxonomies
Displaying your taxonomies involves adding a code and playing and playing about with the WordPress templates. Ensure you back up the site before displaying the taxonomies as you do not want to lose any data due to errors in the process. There are two easy steps to be followed to display the taxonomies effectively.
Step 1: Where are you going to display your code?
The first step involves deciding where you are going to place the code. To hack this, you need to have some WordPress’ template hierarchy. You will be required to add code lines to each template the custom taxonomy should display. More than often, you will amend the content.php or single.php in your template-parts folder. Theoretically, one can add custom taxonomy to their template file of choice.
In case you cannot easily find the template you want to put in the custom taxonomy, there is the Which Template Am I plugin that is very resourceful. It will show you the template paths in your browser. After you are now sure where the code should go, the next step is adding it.
Step 2: Add the code where the taxonomy will be displayed
In this step, you have to access your theme’s files through the SFTP. Click on the template you chose in the step above. After that, look for the perfect spot in that file where the taxonomy should display. Paste the code below at that point:
<?php the_terms ( $post->ID, ‘topics’. ‘Topics: ‘, ‘, ‘, ‘ ‘ ) ; ?>
After saving the changes, the taxonomy should be displayed on the WordPress website. You can make some adjustments, but it should be a clickable link. The taxonomy should be pretty visible. The above code can be added in other files such as index.php. archive.php and virtually any other place. Understand that by default, these custom taxonomies make use of the archive.php template to show your posts. However, one can curate a {taxonomy-slug}.php as a custom archive display.
Adding Taxonomies to Your Custom Posts
How to Create Custom Taxonomy in WordPress
Tags and categories are practical and remarkable; however, what do you do if you need other formidable ways to organize your content meticulously? How can this be possible? As it is a WordPress website, it is always developer-friendly and user-friendly. The site has given developers and users a way to create a specific taxonomy easily. There are several avenues to develop this custom taxonomy.
However, with this approach, one must choose the tool/plugin they want to use first. There are plenty of plugins to choose from, and you can select what seems to work best for you. However, it is crucial to look at the online reviews about that plugin. It should have an outstanding reputation and be recommended by many who have used it before.
Step 1: Create a blank taxonomy and then populate the spaces
First, you have to install and then activate your plugin you have settled on. Remember, do not just pick any for the sake. After activation, go to Pods Admin and then Add New in the WordPress dashboard. The next step is selecting Custom Taxonomy from the drop-down list named Content-Type.
You will then create a plural or singular name for the new taxonomy like ‘Books’ or ‘Book.’ On your webpage, you will also come across an Advanced menu bar though hidden. After filling up all the required fields, tap on Next Step and then click on Configure screen.
Step 2: Assign and then save the taxonomy
Your computer will now take you to an Edit Pods screen, where a success message should be displayed. The computer will allow you even further to customize that taxonomy. You can assign a specific post type to it or make it hierarchical like in different categories. You can now display it on the posts menu, just like tags and categories. Now navigate to the Admin UI screen and fill out the Menu Namespaces.
After that, head over to �Advanced Options’ icon and tick the box adjacent to Posts found in the menu, Associated Types Post. Save all the changes you make, and your taxonomy will appear with others ready to be utilized. That is how you set it up using a plugin.
Creating Custom Taxonomy With Code
People who would not wish to use a plugin to create their custom taxonomy are free to use code. Most developers find doing it manually relatively easier, and this is the route to follow if you don’t want to use a plugin. In this section, you will learn the simple steps to take to create a specific taxonomy using code.
Step 1: Know whether you want the Non-Hierarchical or Hierarchical Taxonomy
First, you need to decide whether the taxonomy you want should be non-hierarchical or not. Don’t know the difference? Here are some quick facts about the two:
- Non-Hierarchical – this taxonomy resembles the tags meaning that you have to define one term that is then assigned to the content. With this taxonomy, you do not create child terms.
- Hierarchical – this taxonomy will enable you to have child and parent terms just like with categories. A good example is, you could be having Beginners as parent term and kid terms as Themes, Marketing, and Plugins.
This decision is very crucial since the code needed for each taxonomy is very different.
Step 2: Edit the functions.php program
Regardless of the taxonomy you decide to use; you still have to access the functions.php program. However, before you do this, there are a few things you need to look into:
- Ensure you backup everything on the site before you carry out any of these activities.
- Make use of a child theme that will enable you to preserve the changes you made while updating the WordPress website and the themes.
- Ensure you have some background on Secure File Transfer Protocol (STFP)
- After that, log in to the WordPress website via SFTP and search for the functions.php program. You can locate it in the primary theme’s folder. Look for it and then open it. Depending on the type of taxonomy you choose, you can input any of the below codes:
Non-hierarchical
// Hook it init action then call create_topics_nonhierarchical_taxonomy when it firesadd_action( ‘init’, ‘create_topics_nonhierarchical_taxonomy’, 0 );function create_topics_nonhierarchical_taxonomy() {// Add Labels to the GUI and $labels = rarray ( ‘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’ ), ); // Register the non-hierarchical taxonomy similar to a Tag register_taxonomy(‘topics’,’books’,array( ‘hierarchical’ => false, ‘labels’ => $labels, ‘show_ui’ => true, ‘show_in_rest’ => true, ‘show_admin_column’ => true, ‘update_count_callback’ => ‘_update_post_term_count’, ‘query_var’ => true, ‘rewrite’ => array( ‘slug’ => ‘topic’ ), ));}
Hierarchical
// Hook to the init action then call create_book_taxonomies when it firesadd_action( ‘init’, ‘create_subjects_hierarchical_taxonomy’, 0 );// Create a specific taxonomy name for your posts function create_subjects_hierarchical_taxonomy() {// Add a new taxonomy and make it hierarchical, like Categories// Translate the GUI and $labels = rarray( ‘name’ => _x( ‘Subjects’, ‘taxonomy general name’ ), ‘singular_name’ => _x( ‘Subject’, ‘taxonomy singular name’ ), ‘search_items’ => __( ‘Search Subjects’ ), ‘all_items’ => __( ‘All Subjects’ ), ‘parent_item’ => __( ‘Parent Subject’ ), ‘parent_item_colon’ => __( ‘Parent Subject:’ ), ‘edit_item’ => __( ‘Edit Subject’ ), ‘update_item’ => __( ‘Update Subject’ ), ‘add_new_item’ => __( ‘Add New Subject’ ), ‘new_item_name’ => __( ‘New Subject Name’ ), ‘menu_name’ => __( ‘Subjects’ ), ); // Register the taxonomy register_taxonomy(‘subjects’,array(‘post_type’), array( ‘hierarchical’ => true, ‘labels’ => $labels, ‘show_ui’ => true, ‘show_in_rest’ => true, ‘show_admin_column’ => true, ‘query_var’ => true, ‘rewrite’ => array( ‘slug’ => ‘subject’ ), ));}
You can decide to alter the placeholder names to other names that will suit your custom taxonomy. One can also switch the post-type in the register_taxonomy function. After saving your changes, there is still some work left before you can display the taxonomy.
Creating Custom Taxonomy in WooCommerce
The best part of this approach is that it is no different from the plugin approach already discussed. First, you have to ensure that your products are already uploaded to the WooCommerce store before creating this custom taxonomy. The only change you will make is when you navigate to Advanced Options screens. This time do not tick Posts (post) but rather tick Products (product). Save all the changes made, and your taxonomy is okay for use.
What WordPress Does with Taxonomies?
WordPress does several things with built-in taxonomies. These may include:
- Building a link to a one taxonomy listing page found on the posts – attaching the in-built terms to the post ensures WordPress displays a clickable term name when the post is viewed on the front end. Typically, the name of the term is placed under the name of your post. Clicking on this term directs you or the user to your term listing page.
- Formulate a single term listing page – your WordPress website will create a term for your listing page. The URL will be a combination of the taxonomy name and the term name. For example, if the website has a ‘category’ called ‘featured,’ its URL will be /category/featured. This page is created to be a listing page for the website.
- Add terms to your navigation menu – all categories and tags are fixed to the navigation administration menu where you are to build and define the main menu. That allows the user to link most of the terms used more often in the website.
- List the terms inside widgets – your WordPress website comes with some widgets that allow the user to add content to the widget areas on their site; these could be footers and sidebars. Both ‘tag cloud’ and ‘categories’ widgets will have your terms, which will be displayed as clickable links.
Register_Taxonomy Function
The function comes in three values i.e. $taxonomy, $args and $object_type. They are as discussed below:
- $taxonomy – it refers to the name of the taxonomy one is creating. WordPress will call their taxonomies ‘tags’ and ‘categories,’ but you may decide to give your taxonomies another name like ‘member.’ However, the name must not exceed 32 characters in length and should only use the underscore and letters. Other than that, the website will not accept that taxonomy name.
- $object_type – it bears the name of the post type to be attached to your taxonomy. WordPress posts have both tags and categories attached. Here you are at liberty to add this new taxonomy to an old post type or a specific post you had written just waiting for upload. There are two options:
- A single string that represents the post type name like $object_type = ‘post’
- Several strings the post types name like (‘post’, ‘page’)
- $args – they are arguments based on various options for this new taxonomy. Multiple options can be set. Some of these options are optional, while others are mandatory.
- Label – it is the plural name of the taxonomy you create if it is a membership taxonomy.
- Labels – an array of different values and names for your taxonomy. Typically, they are applied in the administration areas for management purposes. The array categorizes all labels used for that taxonomy. In case you do not fill out this part, WordPress will use the label value. The non-needed values can be defaulted.
- singular_name – typically the single term used to name the taxonomy
- name – it is the plural name of your taxonomy
- all_items – here one can view all items from your taxonomy
- menu_name – it is the text that is shown on your WordPress administration back-end, to the left side of the administration menu
- view_item – viewing only one term from your taxonomy
- update_item – updating one taxonomy
- add_new_item – when you add a new term text
- parent_item – usually used with hierarchical taxonomies to set parent$taxonomy_name
- search_item – it is the search text one uses when looking through their taxonomy
- parent_item_colon – it is the same as the parent_item, but you add a colon at the end
- popular_items – it is the popular term name and is found at the back-end administration section if you use a non-hierarchical taxonomy.
- separate_items_with_commas – usually, the text is shown for the non-hierarchical taxonomies. For the individual posts, it is shown on the meta box of your taxonomy
- choose_from_most_used – text is also indicated for the non-hierarchical taxonomies located at the bottom of the meta box. When one clicks on it, it pops a listing of the most used terms
- add_or_remove_items – the text will only display after your JavaScript has been disabled in the taxonomy meta box. Usually used in non-hierarchical taxonomies
- not_found – the text is shown in non-hierarchical taxonomies in the meta box. All you do is click on ‘Choose From Most used’ text. The website will check for the most used words, and if there are not available, the not_found text appears
- show_ui – the term decides whether WordPress should display an administration area for the taxonomy. If it is not available, the taxonomy will not have any place to manage the terms
- public – it determines if the taxonomy will be displayed queried against
- show_tagcloud – determines if your website will add your taxonomy terms in the tag cloud widget
- meta_box_cb – it lets the user specify the ideal function to output the design of the meta box for the taxonomies in the single posts
- show_admin_column – it determines if the taxonomy terms will be displayed in a new column for the post listings. If you set it to ‘true, it will display a new column for the specified post type and attached terms
Taking Taxonomies Further
There are many things you can achieve with custom taxonomies, like adding image icons for the terms or showing your terms in a sidebar, etc. You are also at liberty to enable the RSS feed. For those looking to customize their custom taxonomy layout, you can try Divi or Beaver Themer. They are great themes that will let you customize your page layout without coding.