When running a WordPress powered blog, we often make use of categories and tags to sort items within a blog, so as to help improve our users reading experience. However, many WordPress users (especially beginners) don’t know that both ‘Category’ and ‘Tag’ are two of the most popular taxonomies – that are used by WordPress users on a regular basis.
What do we mean by taxonomy in WordPress?
Taxonomy, in general, is a way that help in grouping related things (ideally posts and pages) together. By default, WordPress comes with four built-in taxonomies such as:
- Category: It helps in grouping posts together in multiple categories.
- Tag: Technically, there isn’t any difference between a category and a tag. However, the later comes with more free form.
- Link Category: It helps in categorizing the links inside a post/page. Also, it defines the groups of links that you need to show in sidebars and other places.
- Post Formats: It is a piece of information that a WordPress theme can use for customizing how a post is being presented to users.
WordPress built-in taxonomies can help group your posts together. But, what if you want to group custom post types? To accomplish such a task, you can create custom taxonomies and bring the custom groups all together under one roof.
What is Custom WordPress Taxonomy and how it increases usability?
The release of WordPress version 3 introduced custom taxonomies. They help in increasing website usability to a great extent, as it renders the ability to structure huge amount of content in a well-organized manner. Most importantly, custom taxonomies help group your posts in a more specific way compared to categories and tags. This ultimately helps users search for the content – that they want to view. And, the ease with which any user can look for a piece of content helps deliver an optimal user experience (UX).
So, now that you have understood what exactly custom taxonomy is in WordPress, let us now understand the process of creating one.
Understanding the Process of Creating a Custom Taxonomy
There are three different ways that help in creating and adding a custom taxonomy in a WordPress site:
- Manually write code to create a custom taxonomy.
- Use tools such as GenerateWP for generating code automatically.
- Use plug-ins for creating custom taxonomies.
Perhaps, most of you might prefer choosing the last option, as a plugin can help create and add custom taxonomies in the WordPress backend (admin area) without having to write any code. But, using a plugin might not help meet your exact project requirements.
However, in case you plan on implementing any one of the other two techniques, consider creating a custom plugin. That’s because, it will help you add the code in your theme’s functions.php file in a relatively easy way.
Now, for creating a custom plugin, you just need to copy the following code and paste it into any text file. But, before that remember to add the below line of code at the top of the file wherein you would like to use that custom plugin.
/* Plugin name: Custom Taxonomy */.
Below is a code snippet that will help create a custom plugin, supporting custom taxonomy named “Readers”.
<?php
/* Plugin Name: Readers Taxonomy */
if ( ! function_exists( 'readers_init' ) ) {
// This function helps register a custom taxonomy
function readers_init() {
// function for creating a new taxonomy
register_taxonomy(
'readers','post',
array('label' => __( 'readers' ),
'rewrite' => array( 'slug' => 'reader1' ),
'capabilities' => array(
'assign_terms' => 'edit_guides',
'edit_terms' => 'publish_guides')));
}
// Hook into the 'init' action
add_action( 'init', 'reader_init' );
}
?>
Note: The above code should be added in your theme’s “functions.php” file.
Let us now understand what the above code does:
- First of all, we’ve defined a taxonomy called as “Readers” that helps define the work that users will perform for a single post.
- Secondly, we have declared a rewrite slug for changing the url into ‘/reader1/’ instead of ‘/reader/’. In addition, a capability line has been added. In case you forget to assign your readers with some capabilities, then WordPress will assign default capabilities to its readers.
Using Custom Taxonomy in WordPress
The above code will help in registering and creating a custom taxonomy. In fact, you’ll find that the custom taxonomy has already been added within a meta box on WordPress posts. You can view the newly created meta box just below the “Posts” menu in the sidebar section of your website’s admin area as follows:
Now to make the best use of the custom taxonomy, you should consider adding a few terms to it. Doing so, will make it easier for your readers to find the category that contains the post they want to view. For example, you can create two terms, namely “Featured Readers” and “Standard Readers” that will be added to your custom taxonomy. Next, use the new meta box for your taxonomy to add those terms. This can be achieved using this code snippet:
function display_readers_taxonomy_terms($post_id){
//fetch newly created terms assigned to this post
$reader_terms = get_the_terms($post_id,'readers');
//assign taxonomy terms to the post
if($reader_terms){
echo '<div class="readers-terms-meta">';
echo '<span class="term-title"> Reader Terms: </span>';
//loop through each term
foreach($reader_terms as $term){
//collect your taxonomy terms information and display it
$term_name = $term->name;
$term_link = get_term_link($term,'readers');
echo '<a href="' . $term_link . '">';
echo '<span class="term">' . $term_name . '</span>';
echo '</a>';
}
echo '</div>';
}
Once you’ve added this code snippet in the functions.php file, simply call the following function in your theme’s content.php file:
<div class="entry-meta">
< ?php display_readers_taxonomy_terms($post->ID); ?>
< ?php twentyfifteen_entry_meta(); ?>
< ?php edit_post_link( __( 'Edit', 'twentyfifteen' ), '<span class="edit-link">', '' ); ?>
</div><!-- .entry-meta -->
Now when you will open any single post, you will see your custom ‘Readers’ taxonomy terms as shown in this image:
Conclusion
There are several different ways that can help improve user experience (UX) of your WordPress site or blog. One of the WordPress core features that can help achieve such an objective is custom taxonomies. Creating custom taxonomy helps make your website content highly organized. In this post, we have discussed how you can create and make use of custom taxonomy in a WordPress install.
- Custom Taxonomies to Improve Your WordPress Site’s UX - 4 January, 2016