Almost every WordPress user makes use of taxonomy while blogging on their website, but the majority of them don’t actually know that they’re using it. As site owners or bloggers, you’ll most likely be caught up with things such as selecting the right theme, choosing useful plugins for your blog and so on. Of course, all these factors are crucial to your site’s success, but there is something more that can help in increasing the usability of your website known as: taxonomies.
Taxonomies, in general, provides a way to group things together. In WordPress, taxonomies help in grouping post types and custom post types together in an organized manner.
This eventually helps users to view the content in a convenient way. By default, every WordPress install comes loaded with 4 types of taxonomies, namely: categories, tags, link-categories and post formats.
But, what if you want more flexibility in organizing your website content that built-in taxonomies cannot provide? Well, then you can create a custom taxonomy to meet your desired goals. Through this post, I’ll help you explore about custom taxonomies. In addition, I’ll make you understand the process of creating a new taxonomy for your site.
Custom Taxonomies: An Overview
According to WordPress Codex:
“The ability of creating custom taxonomies was introduced with WordPress 2.3 version, however, this feature was rarely used until the release of WordPress 2.9 version.”
Creating custom taxonomies will help you group your WordPress website post types, so as to make it easy for users to find and view the piece of content they’re interested in. Let’s suppose, for example, you want a separate section underneath your Posts menu, including a list of featured posts for your users. In that case, you can create a custom taxonomy with the name “users” and add a term named “Featured User” to it.
Helping users locate content easily will provide them with an enhanced experience, which will eventually encourage them to visit your site again and again.
Understanding the Process of Creating a Custom Taxonomy
When it comes to creating and adding a custom taxonomy in the site, there are 3 different ways that can help you achieve such an objective, such as:
- Hand code manually.
- Generate custom code automatically using Generate WP.
- The most obvious way to create custom taxonomies is to utilize WordPress plugins, such as Simplify Post Taxonomy, MB Custom Taxonomy, etc.
If you can’t code, you should prefer using a plugin over using a tool to auto-generate code. With a plugin, you’ll be able to create custom taxonomies and embed them in your WP site back end with no need of fiddling with the code. But, in case you’ve good coding skills, then I’ll recommend you to add the following code in your theme functions file or any site-specific plugin.
Here’s an example of a code snippet that helps create (or register) a custom taxonomy named “Users”:
//WordPress action hook that makes a call to create_users_taxonomies when it fires
add_action( 'init', 'create_users_taxonomy', 0 );
//function to create custom taxonomy named as Users that gets added to posts
function create_users_taxonomy() {
$labels = array(
'name' => _x( 'Users', 'taxonomy general name' ),
'singular_name' => _x( 'User', 'taxonomy singular name' ),
'search_items' => __( 'Search Users' ),
'all_items' => __( 'All Users' ),
'parent_item' => __( 'Parent User' ),
'parent_item_colon' => __( 'Parent User:' ),
'edit_item' => __( 'Edit User' ),
'update_item' => __( 'Update User' ),
'add_new_item' => __( 'Add New User' ),
'new_item_name' => __( 'New User Name' ),
'menu_name' => __( 'Users' ),
);
// Now register the taxonomy
register_taxonomy('users',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'user' ),
));
}
Note: Add the above code in your website theme’s functions file.
Once the above code is executed, it will create a new taxonomy named as “Users” and will attach it to the Posts menu in your WordPress website dashboard, as shown in the image below:
How Can You Make Use of the Custom Taxonomy?
Now that you’ve registered your custom taxonomy, you’ll be able to see a new meta box on your WordPress posts. You can find this meta box on the right-hand side of your admin dashboard screen’s sidebar, under the Posts menu. In order to make the best possible use of your newly created taxonomy, all you need is to add some terms to the taxonomy to make it easy for users to understand which category a user belongs to.
For example, you can create terms such as Featured Users, Standard Users, etc. You can add these terms to your custom taxonomy, by editing the post to which your custom taxonomy was attached. And then, use the taxonomy meta box for adding all the terms.
Once you’ve added your terms to a custom taxonomy, you’re left with displaying your newly created terms on single post page. To do so, add the below-provided code snippet in your functions.php file:
function display_user_taxonomy_terms($post_id){
//fetch all terms assigned to this post
$user_terms = get_the_terms($post_id,'user');
//assign your Users taxonomy terms (if any) to this post
if($user_terms){
echo '<div class="user-terms-meta">';
echo '<span class="term-title"> User Terms: </span>';
//loop through each term
foreach($user_terms as $term){
//gather information about your taxonomy terms and display it
$term_name = $term->name;
$term_link = get_term_link($term,'user');
echo '<a href="' . $term_link . '">';
echo '<span class="term">' . $term_name . '</span>';
echo '</a>';
}
echo '</div>';
}
After adding the above function in your functions file, call the below function inside your content file (i.e. content.php):
<div class="entry-meta">
< ?php display_user_taxonomy_terms($post->ID); ?>
< ?php twentyfifteen_entry_meta(); ?>
< ?php edit_post_link( __( 'Edit', 'twentyfifteen' ), '<span class="edit-link">', '' ); ?>
</div><!-- .entry-meta -->
Whenever you’ll open up your single posts, you’ll see that your “Users’ taxonomy terms will be displayed above a post standard category as shown in the image below:
Conclusion
If you want to organize your WordPress blog content, then I’ll advise you to become familiar with the concept of custom taxonomies. Hope that this post will clear your doubts about custom taxonomies and can make you understand the process of creating one for your WordPress website.
This custom post and custom taxonomy good for SEO or not. ?