Custom Taxonomy Archives

Custom taxonomies are an important part of any WordPress theme, especially when it comes to portfolios. However, many themes do not adequately support custom taxonomies in their archive templates, which can lead to PHP errors when viewing a taxonomy archive. In this tutorial, we will show you an easy way to avoid this problem and provide complete support for custom taxonomies in a single template file.

Assuming that all category, tag, and taxonomy archives are displayed with your single archive.php file, as opposed to separate files for category.php, tag.php, etc., most theme developers will place something like this at the top of the archive.php file:

Or this:

Or a combination:

While these functions are perfectly fine to use for categories and tags, they do not work with custom taxonomies and will actually result in an error being thrown (if not using an is_category/is_tag conditional).

However, there is a better way to handle custom taxonomies. If we look at the source code for the tag_description() function, we will see that it is actually just a reference to the similar term_description() function, but with the “post-tag” parameter set as default.

The term_description() function is really nice because it will actually detect the current taxonomy type that is being displayed and display the description for the term from the proper taxonomy, instead of throwing an error like tag_description() does.

There is also a great function for the taxonomy term title that functions the same way: single_term_title()

This means that we can simply use the following in our archive.php and it will work for all taxonomies:

This is much better than creating multiple files to deal with the different taxonomies (unless you’re creating custom layouts for each) and is also much safer. By using these functions, you can ensure that your theme fully supports custom taxonomies and avoids any potential errors.

Stay in Touch


Related Articles