WordPress Guides

WordPress Taxonomies: The Ultimate Guide

WordPress taxonomies are something that many WordPress developers never use. Some have never even heard of a WordPress taxonomy. When WordPress 3.0 introduced hierarchical taxonomies nearly a decade ago, taxonomies became an even more useful tool in the toolkit of WordPress developers. On a high level, we can think of taxonomies as ways of grouping things together.

Avatar photo
SolidWP Editorial Team

WordPress taxonomies are something that many WordPress developers never use. Some have never even heard of a WordPress taxonomy.

When WordPress 3.0 introduced hierarchical taxonomies nearly a decade ago, taxonomies became an even more useful tool in the toolkit of WordPress developers. On a high level, we can think of taxonomies as ways of grouping things together. Within the WordPress platform, a taxonomy is a mechanism for grouping things like posts, custom post types, or links.

In this guide to WordPress taxonomies, we’ll take a look at everything you need to know about WordPress taxonomies and how you can use them to improve your website structure and UX. Let’s dive in.

What Is a WordPress Taxonomy?

If you look at the official WordPress Codex, you’ll see that a WordPress taxonomy is defined as “a grouping mechanism for all of your content.” [pullquote]In a nutshell, a WordPress taxonomy allows you to group posts based upon characteristics that the posts share.[/pullquote]

As an example, say you’re running a blog about popular music from the last 40 years. There’s a good chance that you’ll want to group your blog posts into specific genres. A taxonomy is what allows you to do these kinds of groupings. Taxonomies help improve UX (user experience) by making it an easier and more directed experience when a user is navigating your blog.

WordPress taxonomies

4 Types of WordPress Taxonomies

The two most frequently-used taxonomies in WordPress to group your posts are tags and categories. Both of these are under the same overall umbrella of taxonomies.

In total, WordPress has four taxonomies that you can utilize when grouping your blog posts.

Here, we’ll look at each one of them.

1. Categories

Category taxonomies allow you to group posts into different hierarchical categories. You can group posts with Categories in two different ways.

The first option is to login to your WordPress dashboard and navigate to Posts > Categories. Here, you’ll be able to define categories and add in the define child and slug categories.

The second way to group posts using categories is to be in your WordPress dashboard and navigate to Posts > Add New. When you look at the panel next to the visual editor, you’ll see that you can add your categories directly from this location.

2. Tags

A tag is similar to a category in that it allows you to group posts. This difference is that tags don’t have any hierarchical structures.

Tags are simple parameters that put related posts together. They focus on the small details of your blog content instead of the overall theme.

Let’s go back to your theoretical music blog for a minute. Pretend that you want to make genres and subgenres for the content of your music blog. The best way to do this is by using a category taxonomy for the genre “90’s Hip Hop Stars.” This grouping taxonomy will support a hierarchical structure.

Then, to break down your content further into subgenres, you can add tags to your content for specific artists, such as “Snoop Dogg” and “Ice Cube.”

The best part about grouping content using tags is that the grouping will include any of your content that includes the “Snoop Dogg” tag, for example.

In other words, even if one particular piece of Snoop Dog content is in a different category than “90’s Hip Hop Stars” and happens to fall in the category of “Artists That Never Go Away,” all Snoop Dog content is still grouped together with the tag.

To create tags, follow a similar path as creating categories. It can be done by either navigating to Posts > Tags, or right in the panel next to your visual editor.

You can also define the slug and the description in these areas.

It’s important to note that defining a category is mandatory for all of your blog posts. Tags, however, are completely optional.

All posts need to have at least one category in order to publish. WordPress does, by default, automatically put posts into the category of “Uncategorized” if you don’t define a specific category.

Remember that the category “Uncategorized” is still a category.

The remaining two taxonomies aren’t visible to your site users. Although they aren’t frequently used, they’re definitely worth mentioning and considering how they could be used on your WordPress site.

This is a WordPress taxonomy that allows you to categorize links.

If you’re the type of blogger that links to a lot of different sources, both internal and external, on your blog posts, you’ll find link category taxonomies to be extremely useful.

4. Post_Format Taxonomies

The post_format taxonomy allows you to categorize posts based on types – videos, audio, standard and more.

You’ll see this panel directly next to the WordPress visual editor.

WordPress Taxonomy Template

When site visitors click on a hyperlink that points to a tag, category or custom taxonomy, WordPress will display a page of posts that are in reverse chronological order, filtered by that particular taxonomy.

The display page is generated using your index.php template file, by default. What this means is that you can create an optional template file that will refine and override the index.php template files.

Here, we’ll explain how you can create and use these taxonomy templates.

Taxonomy Template Hierarchy

The Template Hierarchy is what WordPress uses to determine the order that it displays posts.

The category.php, taxonomy.php and tag.php templates allow the posts that are filtered by taxonomies to be looked at differently than unfiltered posts or those filtered using a different taxonomy.

Remember that “post” refers to any post type, such as pages, custom post types or posts. Basically they’re the files that allow you to target specific WordPress taxonomies or taxonomy terms.

Examples of these taxonomy template files would be:

  • taxonomy-{taxonomy}-{term}.php
  • taxonomy-{taxonomy}.php
  • tag-{slug}.php
  • tag-{id}.php
  • category-{slug}.php
  • category-{ID}.php

This could specifically allow you to format all of your posts within an animal taxonomy named “News” on a page with a different look than your posts that are filtered within other categories.

The archive.php taxonomy template will provide the most general form of control. It provides a specific layout for all of your archives. The page displays a list of your posts.

Category Hierarchy

When it comes to categories, WordPress will look at the category-{slug}.php file. If there isn’t one, WordPress then tries to find a file for the next sequential hierarchical level, which is category-{ID}.php, and so forth.

When WordPress doesn’t find an archive.php template file or other specialized templates, it’ll automatically revert to its default behavior by utilizing index.php.

The category hierarchy looks like this:

1. category-{slug}.php: As an example, if the slug for a category is named “pepperoni,” WordPress would seek a file that’s named category-pepperoni.php.

2. category-{ID}.php: As an example, if your category’s ID is “8,” WordPress looks for the file named category-9.php.

3. category.php

4. archive.php

5. index.php

Tag Hierarchy

With tags, WordPress will look for a specific tag-{slug}.php file. If WordPress can’t find it, it’ll look for the needed file in the next hierarchical level, which is tag-{ID}.php and so forth.

When WordPress doesn’t find any archive.php template files or specialized templates, it’ll revert to its default behavior using index.php.

The tag hierarchy looks like this:

1. tag-{slug}.php: As an example, if the slug of the tag is called “drpepper,” WordPress looks for the filed that’s named tag.drpepper.php.

2. tag-{id}.php: As an example, if your tag’s ID was “3,” WordPress looks for the file named tag-3.php.

3. tag.php

4. archive.php

5. index.php

Custom Taxonomy Hierarchy

Custom taxonomy hierarchies work a lot like tag and category hierarchies discussed above.

WordPress will first look for taxonomy-{taxonomy}-{term}.php. If it doesn’t find it, it’ll look for a file that’s next in line in the hierarchy, which is taxonomy-{taxonomy}.php, and so forth.

When WordPress doesn’t find any archive.php template files or specialized templates, it’ll revert to its default behavior using index.php.

The custom taxonomy hierarchy is as such:

1. taxonomy-{taxonomy}-{term}.php: As an example, if your taxonomy is called “taxes,” and the same taxonomy’s term is “statelevel,” WordPress looks for the file called taxonomy-taxes-statelevel.php.

2. taxonomy-{taxonomy}.php: As an example, when a taxonomy is called “taxes,” WordPress looks for the file called taxonomy-taxes.php.

3. taxonomy.php

4. archive.php

5. index.php

How To Create Taxonomy Template Files

Where do you start after you decide that you want to create custom designs for your content using taxonomies?

It’s best not to begin with a blank file. Rather, if it exists, simply copy the next file within the hierarchy.

Assuming that an archive.php is already created, create a copy and call it category.php. Then modify that file to meet your specific design goals and needs.

If you don’t already have the archive.php file, you can use the index.php file in your theme, copy it, and use it as your starting point.

When creating any taxonomy template file, you can follow the same identical procedures. Simply use a copy of the category.php, index.php, tag.php or archive.php as the starting point.

How To Add Text To Your Category Pages

If you want to add text to your category pages, you’ll have several ways of doing it.

Static Text Above the Posts

This is what you’ll use when you want static text (text that always remains the same for all posts in all categories) displayed prior to the list of your posts on category page(s).

Open up the file. Above The Loop section of the Template file, simply insert this code:

<p>
This is some text that will display at the top of the Category page.
</p>

The text will only display on archive pages displaying the posts within that specific category.

How To Put Different Text on Some Category Pages

If you’re looking to display different text that’s based on the specific category page that your visitor is using, consider adding some default text directly to your main category.php file.

You can then create some unique category-{slug}.php files that each have their own version of text. The problem is that this would create a lot of theme files.

To counter this, use conditional tags.

The following code would again be added right after The Loop:

<?php if (is_category('Category A')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_category('Category B')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages,
I could be left blank</p>
<?php endif; ?>

This code will:

  • Check that a user has requested Category A. If they have, it’ll display the first part of the text
  • Check for Category B is the site visitor didn’t make a request for Category A. If they did, it’ll display the second part of the text
  • Display default text if neither A nor B were requested

And you’re all set.

How To Display Text Only on the First Page of Archive

If you have too many posts to fit on a single archive page, the category will be split into several pages. In this case, you may want to display static text for users on the first page of results.

To accomplish this, you want to use a PHP “if” statement that looks for the value of the $paged WordPress variable.

This is what you want to paste in above The Loop:

<?php if ( $paged < 2 ) : ?>
<p>Text for first page of Category archive.</p>
<?php else : ?>
<?php endif; ?>

By using this, the code will ask whether the displayed page is the first one of the archive. When it is, the text dedicated to the first page will be displayed.

If not, the text for the next pages will be displayed.

Custom WordPress Taxonomies

If you look at the right-hand side of your WordPress visual editor, you’ll see the label “Categories” and “Tags.” But what if you want to have your own custom taxonomy that says “Artists,” for example?

This is where custom WordPress taxonomies come into play. This function allows you to customize all of your taxonomies in order to fully accommodate your development requirements.

As an example, consider a WordPress site for a small neighborhood restaurant. Rather than having a generalized taxonomy called “Categories,” wouldn’t it be better to have custom taxonomies that say “Appetizers” and “Main Dishes?”

Then, each custom taxonomy can be further broken down into sub-categories such as “Pizza” and “Pasta.”

The same concept applies to tag taxonomies. You can have tags for things like “Supreme” or “Lighter Fare.”

Creating Custom WordPress Taxonomies

A custom WordPress taxonomy can be created in two different ways. They can be created through direct coding or with a couple of different WordPress plugins.

Creating a Custom WordPress Taxonomy With Plugins

Using a plugin is by far the simplest way to create custom WordPress taxonomies. In fact, you won’t need much technical skill at all in order to do it.

The best plugins to use for creating your custom WordPress taxonomies are Pods and Custom Post Types UI.

We’ll use the Custom Post Types UI plugin to walk through how creating custom WordPress taxonomies are done.

  1. Download, install and then activate the Custom Post Types UI WordPress plugin.
  2. Navigate to CTP UI > Add/Edit Taxonomies
  3. Fill in the box with the WordPress taxonomy name you wish to use. In this case, let’s say we’re using “Artists” as our category.
  4. Select the post type that you want to apply the new WordPress taxonomy.
  5. Click on the “Add Taxonomy” button near the bottom.
  6. Now, navigate to Posts > Add New and you’ll see the new WordPress taxonomy right next to your visual editor.

You’ll probably notice that your new taxonomy is a tag that’s called “Artists.” But what if you want to create a category rather than a tag? The answer is simpler than you might think.

All you need to do is scroll down a bit and change “Hierarchical” to “True.” Your custom taxonomy is now a category instead of a tag.

Create Custom WordPress Taxonomies by Adding Code To functions.php

If you’re not familiar or comfortable working with code, it’s probably a good idea to stick to plugins when creating custom WordPress taxonomies. But if you’re the type that likes to get their virtual hands a little dirty, this will show you how to add the correct code to your site’s functions.php file.

As an advanced WordPress user, you need only to add a couple lines of code to the WordPress functions.php file within the directory of your WordPress theme.

Note that the code for non-hierarchical taxonomies will be different from hierarchical taxonomies.

Before performing this process, make sure you’re running a WordPress backup plugin that will you allow you to instantly recover your site if anything breaks in the process.

And while you’re at it, it’s a good time to consider the WordPress security plugin that’s running on your site.

Get SolidWP tips direct in your inbox

Sign up

This field is for validation purposes and should be left unchanged.
Placeholder text
Placeholder text
Thanks

Oops something went wrong, please try submitting again

Get started with confidence — risk free, guaranteed

With the major worldwide security hacks and breaches currently happening, it’s important to make sure that you’re using the best tool to keep your WordPress site secure. iThemes Security has you covered.

Now, the examples of the required code for hierarchical taxonomies and non-hierarchical taxonomies are shown below.

Hierarchical taxonomy (category): Example Code

//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' ),
));
}

Non-hierarchical taxonomy (tag): Example Code

//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_topics_non-hierarchical_taxonomy', 0 );

//create a custom taxonomy name it topics for your posts
function create_topics_non-hierarchical_taxonomy() {

// Add new taxonomy, make it non-hierarchical like tags

//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' =false,
'labels' =$labels,
'show_ui' =true,
'show_admin_column' =true,
'query_var' =true,
'rewrite' =array( 'slug' = 'topic' ),
));
}

Then, when you want to have the new taxonomy show up in your WordPress visual editor, open up single.php from within the Editor and copy the following code:

the_terms( $post-ID, 'topics', 'Topics: ', ', ', ' ' );

If the whole process was completed properly, you’ll now see a new taxonomy called “Topic” within your visual editor.

Wrapping Up: Understanding WordPress Taxonomies

As we’ve learned, a WordPress taxonomy is what you use to group content on your WordPress site. Taxonomies can be used as categories for broad content topics, and tags for smaller details within those topics.

You can also create custom taxonomies by using a few specific WordPress plugins or by adding code to the functions.php file.

When you use a WordPress taxonomy strategy in the right way, it’ll boost your site’s user experience and overall performance.

Did you like this article? Spread the word: