What are Custom Taxonomies in WordPress?

tutuploadsDefault_Taxonomies.png

Taxonomy is a common word in biology (and also Drupal!). In science it’s a hierarchy of terms used to classify almost everything. Wikipedia has a full explanation here.

Recently WordPress has adopted the word “taxonomy” as an additional way to organize information. It’s an uncommon word for a common concept.

When you were in school, you picked up a book and opened to the table of contents, and you were looking at the book’s taxonomy. It’s just a way to classify and label things. In a web site, it makes it easier to find thing or show them when you need them. Here’s how it applies to WordPress:

What are the default taxonomies?

tutuploadsDefault_Taxonomies.png

According to the WordPress codex (documentation), WordPress has three built in taxonomies that you’ve probably used already.

1) Categories

The ‘category’ taxonomy lets you group posts together by sorting them into various categories. These categories can then be seen on the site by using ‘/category/name’ types of URLs. Categories tend to be pre-defined and broad ranging. You can have top-level categories as well as sub-categories and sub-sub-categories.

2) Tags

The ‘post_tag’ taxonomy is similar to categories, but more free form. Tags can be made up on the fly, by simply typing them in. They can be seen on the site in the ‘/tag/name’ types of URLs. Posts tend to have numerous tags, and they are generally displayed near posts or in the form of tag clouds.

3) Link Categories

The ‘link_category’ lets you categorize your links. These tend to be used only internally, for organizational reasons, and are not usually exposed on the site itself. They are handy for defining groups of links to be displayed in sidebars and the like.

New! Custom taxonomies

Recently WordPress has added the ability for you to break out of these three taxonomies. No longer are you stuck with just Categories, Tags and Link Categories.

Why would you want more taxonomies? If you were creating a list of movies, for instance, you might want to add Actors and Directors to each post, as in the images below:

tutuploadsNow_you_can_create_custom_taxonomies_to_help_classify_any.png

You can use this taxonomy for anything.

  • If you’re making a product catalog you could add color, size, and price.
  • In a staff phone book you could have each person’s Department, Title, Phone.
  • I’ve seen it used for car dealership websites to add make, model, year and other information about cars.

Creating a custom taxonomy

There are two ways you can do this: you can write a few lines into the theme’s function.php file or you can use a plugin. The plugins are very easy to use. Here are two that I found to work well without a lot of trouble. The advantage to the plugins, is that they also allow you to create and manage custom post types as well as taxonomies. You can also adjust all the parameters using form instead of writing code.

These are just two of several available. If you do not understand taxonomies, however, while these are simple to set up, they are very confusing to use for a beginner. Familiarity with taxonomies and custom post types will save you lots of head scratching. You may have to do some reading. Stick with us, we have more tutorials on this topic in the works.

Step 1: Edit your functions.php file

tutuploadsStep_1_-_Locate_your_functions.php_file_for_editing._Look.png

You can edit it by going to Appearance > Editor > Theme Functions (functions.php) or you can use FTP to download and edit the file.

tutuploadsmedia_1314766202962.png

The functions.php file you need to edit can be found in the theme’s directory. If you don’t have one, you will need to make one. A typical path would be [website_root]/wp/wp-content/themes/twentyten/functions.php.

There is a functions.php file in your main WP directory. DON’T edit that one.

Step 2: Add the hook

// Custom Taxonomy Code Starts here
add_action( ‘init’, ‘build_taxonomies’, 0 );

Add this line to the bottom of the functions.php file. This hooks the ‘build taxonomies’ function to the ‘init’ event. Basically this is the kick starter for the function. You could put this anywhere in the file, but I’m putting it right above the function to keep things in logical order

Step 3: Build the function

function build_taxonomies() {
// code will go here
}

This actually builds the function. You’re going to replace “//code will go here” with the code that describes and controls it. Now you just need to register the taxonomies.

Step 4: Register the taxonomies

tutuploadsStep_4_-_Register_the_taxonomies..png

You will be adding code similar to this in the functions.php file. Replace “//code will go here” in the example code above.

  • register_taxonomy( // starts the registration
  • ‘color’, // This is the name of the taxonomy in the database. No capitals, spaces or punctuation.   
  • ‘post’, //This is type of content that will use the taxonomy. You could have more than one ‘post,page.custom’   
  • array( ‘hierarchical’ => true, // If true, will be multi-level like categories. If False will be single level like tags.   
  • ‘label’ => ‘Color’, // This is the name that will be visible to the public   
  • ‘query_var’ => true, //Allows you to search for any entry with ‘color’ attached.
  • ‘rewrite’ => true ) ); // allows creation of search engine friendly URLs

There are a few more parameters that can be defined for a taxonomy. You can get a complete list and more details in the codex.

Step 5: Repeat for additional taxonomies

tutuploadsStep_5_-_Repeat_for_additional_taxonomies..png

Repeat the code used in Step 4, but change the name of the taxonomy, the type if needed, and the label. You will be able to create as many of these as you want. Above is what the code would look like for multiple taxonomies.

Step 6: Check the taxonomies are active

The final step in this process will be to login to your WordPress site and check that the taxonomies are available inside posts:

tutuploadsNow_you_can_create_custom_taxonomies_to_help_classify_any.png

Instructor

0 0 votes
Blog Rating
Subscribe
Notify of
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Nathan

You posted the code as images rather than text that readers can copy and paste to try out??

Nick

Thanks Nathan. We’ll make adjustments for future versions.

Kind regards,

Nick

Nathan

Thanks Nick! I noticed the tutorial left out an example of how to display the taxonomy once it’s been created. It works in the Admin, but there’s no info on how to output the items onto the site.

Can you post the snippet of code needed to complete the tutorial? Thanks!

Nathan

You’re a genius! This is the first tutorial I found that actually worked! But please repost using code instead of images. It’s painful to retype all the code. Thanks!

Nick

You’re very welcome, Nathan! We’re glad we could help! 🙂

We have a lot more great tutorials available to students of our online classes. Take a look: [url=http://www.ostraining.com/online]www.ostraining.com/online[/url]

Kind regards,

Nick

5
0
Would love your thoughts, please comment.x
()
x