Amber Weinberg: Freelance Web Developer specializing in semantic WordPress, Mobile, CSS and HTML5 Development

The Blog

Creating Custom Taxonomies in WordPress

Posted on 06/17/10 in blog, development about , , ,

I had a client this week that wanted to install WordPress on the CoreCommerce site I recently did for her. When I was checking out the mockups, I noticed a strange section in their sidebar. Basically, they wanted to have four different tag areas with different sections, one for “Materials”, “Colors”, “Country” and one called “Labels” that would house all of them.

My first instinct was to write an email telling them that this couldn’t be done.

Then, I realized that WordPress offers custom taxonomies, so that my next instinct was to tell them they could all be done, except for “Labels”, as WordPress doesn’t actually have a native way of showing all the tags at once. After some thought, I was able to find a solution for that too.

So what, exactly, is a custom taxonomy? The “Tag” feature of WordPress is a taxonomy, or way of categorizing and organizing your site. You can actually add as many taxonomies as you want and they’ll show up in your sidebar when adding a post.

Adding Multiple Taxonomies to Your Posts

Simply add this code to your functions.php file, changing the names of the tag types:

add_action( 'init', 'create_my_taxonomies', 0 );
 
function create_my_taxonomies() {
	register_taxonomy( 'country', 'post', array( 'hierarchical' => false, 'label' => 'Country', 'query_var' => true, 'rewrite' => true ) );
	register_taxonomy( 'colors', 'post', array( 'hierarchical' => false, 'label' => 'Colors', 'query_var' => true, 'rewrite' => true ) );
	register_taxonomy( 'materials', 'post', array( 'hierarchical' => false, 'label' => 'Materials', 'query_var' => true, 'rewrite' => true ) );
}

Displaying The New Tag Clouds In Your Sidebar

Displaying your new tag clouds has to be done manually, but is super easy to implement:

<?php wp_tag_cloud( array( 'taxonomy' => 'country', 'number' => 45 ) ); ?>

Displaying Custom Tags In Posts

To display your custom taxonomy in your posts like you’d normally do at the bottom, simply add this:

<?php echo get_the_term_list( $post->ID, 'materials', '', ', ', '' ); ?>

Displaying All Tags Together

I couldn’t really figure out a way to display all of the tags together, because calling the default the_tags function would only display the tags that were added via the regular taxonomy. Instead, I sort of “hacked” my way into making it work. (If anyone knows a better way to do this PLEASE share!)

<?php the_tags( 'Labels: ', ', ', ''); ?>, <?php echo get_the_term_list( $post->ID, 'materials', '', ', ', '' ); ?>, <?php echo get_the_term_list( $post->ID, 'colors', '', ', ', '' ); ?>, <?php echo get_the_term_list( $post->ID, 'country', '', ', ', '' ); ?>

As you can see, I ended up just manually calling each taxonomy, and separating each with a space and comma. It actually ended up working quite nicely.

About the author
Amber Weinberg specializes in clean and semantic XHTML, CSS and WordPress development. She has over 10 years of coding experience and is pretty cool to work with. Amber is available for freelance work, so why not hire her for your next project?

3 Awesome Comments

  1. TheAL says:

    A similar approach has been one of the driving forces behind Drupal from the get-go, so it’s nice to know it’s in WP for when I need it. I wonder if that’s where they got the idea. *hmmm*

  2. Mark Leong says:

    Thanks for the write-up :)

    To display all the tags in a given taxonomy, you can use the get_terms function (http://codex.wordpress.org/Function_Reference/get_terms).

  3. Lee says:

    You can pass an array of taxonomies as the taxonomy argument to wp_tag_cloud(). Should do exactly what you’re after for your ‘labels’ tag cloud …

Leave a Reply