Posted on 13 September, 2010 By 11 Comments

How to Add Custom Taxonomies to Your WordPress Theme

 

The first burning question here is what the heck is a taxonomy.

Simply, it’s a way to group things together. You are probably familiar with this with your music collection – it can be grouped by genre, artist, release year, etc. You may also remember taxonomies in Biology class when you learned about Linneaus and his classification of plants, animals and minerals.

In WordPress, a taxonomy is a way to group together types of posts.

You are already using three taxonomies that are built into WordPress – categories, tags and link category. Custom taxonomies have been available since 2.3 but haven’t really been practically used since versions 2.9

In the following example, I will be creating custom taxonomies for a video game collection.

The Function

First you need to add a function to your functions.php file to make custom taxonomies available. As always, back up your functions file before making any changes to it. You will also be defining your taxonomies so this would be a good time to make a list of what the taxonomies for your topic would be. For video games, I have condition, developer, genre, release date, operating system and date played. I’m not using title because the title is used in the title field of the blog post. Other possibilities could be publisher, rating, etc.

add_action( ‘init’, ‘create_pc_db_taxonomies’, 0 );
function create_pc_db_taxonomies() {

register_taxonomy( ‘condition’, ‘post’, array( ‘hierarchical’ => false, ‘label’ => __(‘Condition’, ‘series’), ‘query_var’ => ‘condition’, ‘rewrite’ => array( ‘slug’ => ‘condition’ ) ) );

register_taxonomy( ‘developer’, ‘post’, array( ‘hierarchical’ => false, ‘label’ => __(‘Developer’, ‘series’), ‘query_var’ => ‘developer’, ‘rewrite’ => array( ‘slug’ => ‘developer’ ) ) );

register_taxonomy( ‘genre’, ‘post’, array( ‘hierarchical’ => false, ‘label’ => __(‘Genres’, ‘series’), ‘query_var’ => ‘genre’, ‘rewrite’ => array( ‘slug’ => ‘genres’ ) ) );

register_taxonomy( ‘release’, ‘post’, array( ‘hierarchical’ => false, ‘label’ => __(‘Release Date’, ‘series’), ‘query_var’ => ‘release’, ‘rewrite’ => array( ‘slug’ => ‘release’ ) ) );

register_taxonomy( ‘os’, ‘post’, array( ‘hierarchical’ => false, ‘label’ => __(‘Operating System’, ‘series’), ‘query_var’ => ‘os’, ‘rewrite’ => array( ‘slug’ => ‘os’ ) ) );

register_taxonomy( ‘dateplayed’, ‘post’, array( ‘hierarchical’ => false, ‘label’ => __(‘Date Played’, ‘series’), ‘query_var’ => ‘dateplayed’, ‘rewrite’ => array( ‘slug’ => ‘dateplayed’ ) ) );

}

You can register as many taxonomies as you want to use for your subject. You can see that the taxonomy is placed in four different places when it is registered; the actual taxonomy, the label for it, the variable used in queries, and what the slug (what shows up in a url).

Edit Post Screen

After the function and register taxonomies are added, you will have new fields on your Edit Post screen that looks just like the section for Post Tags and are listed below that section.

You will fill in the fields just like you do for tags. In my example, I’ve listed the physical condition of the game as very good and the developer as Psygnosis.

How to Display

So, now we are like great – I have this new data but they don’t show up anywhere. What am I supposed to do with this? You will need to add the following code to the template file where you want the taxonomies to be displayed. In my example, each game is a post so I want the taxonomies to be displayed on the single post page, which is single.php. Again, backup of the file before editing it. Add the following for each taxonomy that you want on the single post page and make sure this code is within the loop. I’ve set mine up to display next to the thumbnail of the video game box, which is above the rest of the content about the game.

ID, ‘condition’, ‘Condition: ‘, ‘, ‘, ” ); ?>

ID, ‘developer’, ‘Developer: ‘, ‘, ‘, ” ); ?>

ID, ‘genre’, ‘Genre: ‘, ‘, ‘, ” ); ?>

ID, ‘release’, ‘Release Date: ‘, ‘, ‘, ” ); ?>

ID, ‘os’, ‘Operating System: ‘, ‘, ‘, ” ); ?>

ID, ‘dateplayed’, ‘Date Played: ‘, ‘, ‘, ” ); ?>

get_the_term_list has 5 parameters and the first two are required. They are ID, taxomony, before, separator, after. There is more information about this in the codex.

Displayed next to the thumbnail the taxonomies look like the following.

The End

If you notice from the screenshot, the taxonomies are links. If you click on them, you will be taken to an archives page that contains all of the posts that fall within that taxonomy. For example, clicking on the genre, adventure, will take you to a list of all adventure games. Custom archives can be made so they don’t look like the default archives, but that will be a post for another day.

So, that’s how custom taxonomies are added to a WordPress theme. Can you think of any subjects, other than videogames, that you would use this sort of post classification system for?

photo credit: denn


Emails for Small Business with Constant Contact

Keep Reading:
Posted In : WordPress Tips

Did you enjoy this article? I can assist you with your WordPress or Facebook project. Contact me and I will provide you with an estimate. You can also connect with me on Facebook or follow me on Twitter. If you are looking for Hosting, WordPress Theme, Newsletter or other recommendations, please view my detailed list.




11 Responses to “How to Add Custom Taxonomies to Your WordPress Theme”


  • Thanks for info. i am trying on my new blog, i will let you know, if i meet problem.

    thanks !!!

    Colleen


  • Very cool, although not quite sure what I’d use it for.


  • Now this is one thing I did not know about WordPress. Probably I’m satisfied with the category function. Perhaps I will be needing this application in the future. :-)


  • Great overview, Kim. I’d heard the phrase before, but like you mentioned at the start, my initial response was WTF? :)

    I think this could be used fr a great many options – ebooks, for example, or white papers (as well as the lists you mention here and in the comments).

    While I’m not sure it’d be suited for my personal blog, I can see a use over at 12for12k for sure (explaining more about the charities, for example).

    Thank you!


    • Hi Danny – Using taxonomies as a way of organizing the information about charities is an excellent idea! I wouldn’t have thought about that.

      I’ve also been thinking about a way of cataloging recipes but that would take a lot of thought about the best taxonomies to use.

      This is something I’ve been meaning to play around with for quite some time – I finally sat down and spent a day with it.

      Thanks!


  • Hello, thanks for this info! I’m planning on adding a directory of sorts to my blog. I saw a template that used taxonomies to create a searchable template and it was awesome.

    This helps get me going in the right directions…awesome!

    Thank you!!!


    • Hi Tony,

      The new version of WordPress also has Custom Post Types built in, which might make it a lot easier to set up the taxonomies. I haven’t spent any time working with them yet.


  • Hi Kim,

    Thank you…I have the latest version so I’ll look into that. Anything that can make it easier :) Have a great day!

    Tony


  • Hi Kim,
    It’s very interesting post, i search something like this but i need two things.
    How can we hide/display the “genre:” if empty field.
    How can i have no link on operating system?
    i try with get_the_term_list or get_terms without result.
    thanks you!


Trackbacks/Pingbacks
  1. 5 Taxonomy Plugins for WordPress | WordPress and Facebook Consultant | (Anti) Social Development
Leave a Comment
You may use: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .


About

Kim Woodbridge is an accomplished Information and Technical Consultant specializing in the entire implementation of a WordPress based website including installation, theme design, upgrades, unique customizations and ongoing site maintenance.

Recent Comments

    • Property Marbella: Hi Kim, Declaring is not fun, I do a draft and my accountant will take care of the rest, the only thing I hope for is t...
    • Tanya: How could that video not put a smile on your face. Thanks!...
    • Dada Mirada: I also user Worpress , i have a using wordpress a over two years, but my site often maintenance...
    • silicon28: I just read this post (some years after the initial comments) and could not agree more! I also try to help folks on the...
    • Dada Mirada: Beautiful Art a your sharing...

Wordpress Services

  • Installation, upgrades and maintenance
  • Conversion of existing html and css templates
  • Theme and plugin recommendations
  • CSS customizations
  • Troubleshooting and tweaks for unique situations
  • Customization for individual blogging goals and needs
  • Training and advice