Installing Custom Taxonomies in WordPress Themes
This guide is meant for individuals who want to learn what custom taxonomies are in WordPress, how to install them and how to display them in WordPress themes. You do not need to be familiar with the WordPress core coding or know the nitty-gritties of PHP programming to understand this guide.
Jump-to Link Box
What are Custom Taxonomies?
Installing Custom Taxonomies
Displaying Custom Post Taxonomies in Your Themes
Customizing and Styling Taxonomies
The Difference Between Custom Taxonomies and Custom Fields
What are Custom Taxonomies in WordPress?
Sometimes WordPress publishers have a need to input custom meta data in the post editor and display it on pages (whether they be the index, archive or single post pages). By custom meta data, I mean meta data other than that which comes standard in most themes (like the post tags and the Category[s]).
Think of a custom taxonomy as something no different than post tags. With post tags, you can insert information in the post editor and the bits of information (the linking post tags themselves) display on your pages when the post is published. With custom taxonomies, you can insert information in the post editor and have that information displayed on pages, just as you do with post tags.
Note: Post tags are, in essence, a custom taxonomy named “Post Tags” in the post editor and their purpose is to organize post listings, displaying them through archive.php.
Installing Custom Taxonomies: A Simple Copy + Paste Method
To install a custom taxonomy in your theme, simply navigate to your template files editor (Appearance –> Editor), click on Theme Functions and past the following code:
register_taxonomy('taxonomy_name', 'post', array(
'hierarchical' => false, 'label' => 'taxonomy_name',
'query_var' => true, 'rewrite' => true));
?>
Note: Make sure that the PHP within your functions.php file is properly closed. The page should end with ?> and you should not have the PHP tags (‘<?php‘ and ‘?>‘) nested within other PHP tags. If you are getting an error when you update posts in the post editor, then you know you improperly updated your Theme Functions file.
You now have a taxonomy registered as “taxonomy_name” and named “taxonomy_name.” Navigate to your post editor by clicking edit on one of your previously published posts and look in the sidebar for your new taxonomy. It should look like this:
Go ahead and enter a value in “taxonomy_name” and click “Add.” Update the post and view it in your browser by clicking the “view post” button in the top of the post editor.
See anything different? You shouldn’t. Because, right now, you’ve stored a value in a custom taxonomy registered as “taxonomy_name,” but you have not specified if, where and how you want the taxonomy to be displayed in your theme’s template files.
Displaying Custom Post Taxonomies in Your Theme
Now that we’ve already created a new custom taxonomy by registering it in functions.php, and put a value in the taxonomy, we just need to alter the theme’s template files so the taxonomy values will show.
First you want to ask yourself this question: where do I want the custom taxonomy to display? If you only want the taxonomy values to display on the frontpage, you will need to edit index.php; if you only want the taxonomy values to display on the frontpage and single post, you will need to edit both index.php and single.php. You may want the values to display only on in the archive; in that case, you will need to edit archive.php. In this guide, I will edit single.php to display my taxonomy values on only single post pages.
Navigating to single.php, I see a bunch of code that, had I not been familiar with theme code, would greatly confuse me. Below is the single.php code for the old WordPress default theme (the one that was replaced by Twenty Ten):
/** * @package WordPress * @subpackage Default_Theme */ get_header(); ?> <div id="content" class="widecolumn"> <div class="navigation"> </div> <div>id="post-"> <div class="entry">Read the rest of this entry ยป '); ?> ' <strong>Pages:</strong> ', 'after' => ' ', 'next_or_number' => 'number')); ?> Tags: ', ', ', ' '); ?> ID, 'taxonomy_name', 'Taxonomy Value: ', ', ', ''); ?> <p class="postmetadata alt"><small> This entry was posted You'll need to download this plugin, and follow the instructions: http://binarybonsai.com/wordpress/time-since/ */ /* $entry_datetime = abs(strtotime($post->post_date) - (60*120)); echo time_since($entry_datetime); echo ' ago'; */ ?> on at and is filed under . You can follow any responses to this entry through the feed.</small></p> <small> // Both Comments and Pings are open ?> You can <a href="#respond">leave a response</a>, or <a rel="trackback" href="<?php trackback_url(); ?>">trackback</a> from your own site.</small> <small> // Only Pings are Open ?> Responses are currently closed, but you can <a rel="trackback" href="<?php trackback_url(); ?> ">trackback</a> from your own site.</small> <small> // Comments are open, Pings are not ?> You can skip to the end and leave a response. Pinging is currently not allowed.</small> <small> // Neither Comments, nor Pings are open ?> Both comments and pings are currently closed.</small> <small> </small> <small> </small> </div> </div> Sorry, no posts matched your criteria. </div>
If you’re a beginner to PHP coding or WordPress theme coding or not even a coder at all, you may be a little alarmed at this point. Don’t worry. The following steps will help you identify exactly where in that mess of code you need to insert the code that references your custom taxonomy.
To identify where you need to place your custom taxonomy code in single.php (or any other template file), simply:
- Open up one of your posts in your browser.
- Press Control-F to open up your browser’s text finder.
- Type in “the_tags” (minus parenthesis) and hit enter. If the search doesn’t find anything, go to step 3.
- Type in “the_category” and hit enter. If the search doesn’t find anything, go to step 4.
- Type in “the_author_posts_link” and hit enter. If the search doesn’t find anything, go to step 5.
- Type in “the_time” and hit enter. If the search doesn’t find anything, you’re fresh outta’ luck!
What you’ve just done is search for references in the code that call for meta information — which is something common to all themes. Most themes will display either one, some or all of the following in single post pages: the tags, the categories the post is contained within, the author link and the time. This is relevant to us because taxonomies are most commonly used to display meta data. Taxonomies are, by definition, custom meta data. And odds are, you’re going to display all of your meta data in one spot, so let’s learn how to display your taxonomy values next to already displaying meta information in your theme.
Note: It is important to understand the difference between meta data and meta tags. The term “meta tags” typically refers to the information on a page (within the HTML meta tag) presented to search engines, consisting of things like keywords, description and other information. Read more about meta tags in WordPress here. Meta data is basic “administrative” information on a page, almost always presented to the human eye, that encompasses such things as the author name, category and date published. Read more about meta data in WordPress here.
In the old default WordPress theme, I’m going to copy and paste the code to display my custom taxonomy right below ‘the_tags’ (<?php the_tags( ‘<p>Tags: ‘, ‘, ‘, ‘</p>’); ?> in full). That way, my custom taxonomy will display right below where my tags (which are a sort of non-custom taxonomy) normally would in my single post pages. Here’s what I’m pasting in:
<?php echo get_the_term_list($post->ID, 'taxonomy_name', 'Taxonomy Value: ', ', ', ''); ?>
“taxonomy_name” represents the name I registered my new taxonomy in functions.php and “Taxonomy Value” is an arbitrary string of text I have chosen to display before the first taxonomy value. Navigating to the post I had already installed a taxonomy_name value on in the post editor (see above “Installing Custom Taxonomies” heading above), I can now see my taxonomy value displayed just below the post tags.
Note: If you are using the WordPress default theme, your results may look much different from mine. I am using the SexyBookmarks plugin to display social sharing icons below each post.
Now that you’ve created, installed and learned how to display a custom taxonomy in your single post pages, you can apply the same concept to other template files like index.php and archive.php. Odds are, you will want to display most of your meta data in the same area, so just follow the numbered steps above to identify where the meta data is and paste in your custom taxonomy code.
Customizing Taxonomies – Naming and Styling
You can obviously change the names of your taxonomies and add new ones with different names by replacing “taxonomy_name” with your own name in the above code examples. Be sure that each taxonomy is registered in functions.php with the same name called in your other template files.
You can style your taxonomies the same way you would your meta data. In the appropriate templates, place a span, list (if it’s within an ordered or unordered list), p or whatever tag around your taxonomy code with a class or id, then style that class or id in style.css.
The Difference Between Custom Fields and Custom Taxonomies
There has been a lot of confusion in the WordPress community as to the difference of custom fields and custom taxonomies. In short: custom taxonomies can be used cross-site while custom fields are only meant to input data specific to one post. For example, data inserted into post tags and categories taxonomies (which come installed default on any WordPress installation) are by nature meant to be re-used cross-site. If I put two posts into the same category (or tag) and have a link displayed on both of the posts to the category (which most themes will do), both posts are using the same data (the category link) which is a taxonomy.
Although taxonomies and custom fields are similar in purpose, the latter has limitations. That is why I recommend using taxonomies rather than custom fields in most circumstances. Even if you don’t plan on using the information cross-site initially, you may have a need to in the future. Tomorrow I will publish a shorter guide outlining everything you can do with custom post taxonomies.
Tags: wordpress help, wordpress theme design




i was follow your step to tried out some custom taxonomies, but i always getting this message below, can i know y?
“Warning: Cannot modify header information – headers already sent by (output started at C:\xampp\htdocs\wordpress\wp-content\themes\colourise\functions.php:7) in C:\xampp\htdocs\wordpress\wp-includes\pluggable.php on line 868″
is it i miss out something?
Make sure there is no white-space in the file. White-space at the end and the beginning of the file (after ?> and before ) causes errors.
[...] and writes about the latest in blogging at Blogtap.net. Check out his tutorial on installing custom taxonomies in WordPress themes. As a blogging consultant, Chris is always willing to answer questions and take up new clients. [...]
I received the same error as han fei. Not sure why. I was following the exect steps as stated in the original post. However I am not sure i understand the usage of it anyways. So i will wait to implement it on a live blog of mine later when some other info is available about this or comments from other users.
[...] Installing Custom Taxonomies in WordPress Themes [...]
Two items. The “Cannot modify header information . . . ” occasionally drives me nuts. Even the spacing fix Gemma mentioned doesn’t always do it. Googling the problem, one source said to place the cursor after the ?> and hold down the Delete key for 15 seconds!!! Not sure how, but this really is a glitch and should be addressed. BTW, the 15 sec fix didn’t work either.
Second, this is the BEST Taxonomy explanation I’ve found. Chris covers important areas other tutorials miss, such as that the taxonomies can be placed in a variety of file types — pages, posts, custom posts, etc. This seems obvious, but I’d come away from other explanations with the notion that they were somehow tied to custom types.
Great job and thank you!