• Blog

    Latest from our Blog

  • Tools

    Our Favourite WP Tools

  • Hosting

    Recommended Web Hosts

  • Coupons

    Get great money off deals

  • Themes

    WordPress Theme Directory

  • Plugins

    WordPress Plugins Directory

  • Promote

    Your WordPress Product

WordPress allows you to categorize your posts and tag them to display your content under different topics and groups. However, WordPress also allows you to create your own Taxonomies other than categories and tags. Just what is a taxonomy though ?

Taxonomies are essentially a way to group things together, WordPress uses the example of animals – you can put them together in groups such as fish or birds, the groups are called “terms”.

We showed you how to create custom post types in WordPress using the example of movies so one popular way of grouping movies together is by using genres, so this becomes the term for our taxonomy tutorial.

In this WordPress Custom Taxonomies Tutorialy we are going to learn about taxonomies and how you can create custom taxonomies and use them with your custom post types to better organize your custom content.

Creating a Custom Taxonomy

Continuing from our previous post, where we created a custom post type for movie reviews, in this post we will create a custom taxonomy for post type movie reviews. You can change the scenario for your own custom post types or use the “posts” as default post type.

Firstly we are going to create a taxonomy “Genre” for our custom post type “movie_reviews” which we created in previous post. Add the following code to your theme’s functions.php to do this:

function genre_init() {
// Registering taxonomy and associating it with custom post type movie_reviews which we created
// in previous tutorial.
  register_taxonomy('genre',array('movie_reviews'), array(

    'hierarchical' => true,
 // This will create categories like taxonomy.
 // Set  it false if you want to use tag like taxonomy

// Below  we define labels for display purposes
    'labels' => array(
    'name' => _x( 'Genres', 'taxonomy general name' ),
    'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Genres' ),
    'all_items' => __( 'All Genres' ),
    'parent_item' => __( 'Parent Genre' ),
    'parent_item_colon' => __( 'Parent Genre:' ),
    'edit_item' => __( 'Edit Genre' ),
    'update_item' => __( 'Update Genre' ),
    'add_new_item' => __( 'Add New Genre' ),
    'new_item_name' => __( 'New Genre Name' ),
    'menu_name' => __( 'Genre' ),
  ),
// some more arguments
// Show UI enables a user interface to manage items in taxonomy
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'genre' ),
  ));
}
add_action( 'init', 'genre_init' );

You can now see your taxonomy under the Movie Reviews menu item :

Gener Taxonomy Added

You can now visit the “Genre” link and begin adding terms for the movie post type – these will function like categories and allow you to specify one or more when adding a movie review.

Adding a Taxononmy

On the “Add Movie” screen you will see any genres you created appear on the right-hand side, you can also add new ones from this screen.

Genres on right

Explanation: The function register_taxonomy has these parameters:

<?php
	register_taxonomy($taxonomy, $object_type, $args);
?>

$taxonomy is the name we want to give to our taxonomy.

$object_type defines what kind of objects or content type will be grouped with this taxonomy. By default it uses posts, but since we have created a custom post type movie_reviews we used it instead. This is an array so lets say if we wanted to use this taxonomy for more than one object we can just add them by adding a comma. We can associate a taxonomy with a custom post type as well as default post types in WordPress.

$args The third portion is an array of arguments, different settings we can define for our custom post type. We have defined an array of labels along with some other settings.

Displaying Custom Taxonomies

In the second step, we need to learn how to display this taxonomy across our website. Just like categories and tags, there are many template tags available which you can use to display taxonomies, retrieve list of objects in taxonomy, create taxonomy cloud, list them in a bulleted list in sidebar, etc. etc.

To display the genres below each movie review inside the loop we use this code:

<?php
	echo get_the_term_list( $post->ID, 'genre', 'Genres: ', ', ', '' );
?>

Tip: The above code can be placed in any template file but ideally the template files you have created for the specific custom post type, like in this example we will be posting it in archive-movie_reviews.php and single-movie_reviews.php which we created in our previous tutorial.

4

The next thing you might want to do is to display objects in your custom taxonomy in sidebar or any other widgetized area. To do this I recommend using Taxonomy Widget plugin by Michael Fields.

You can also use the code below to display a list of items in genres custom taxonomy:

<?php
$args=array(
  'name' => 'genre'
);
$output = 'objects'; // or objects
$taxonomies=get_taxonomies($args,$output);
if  ($taxonomies) {
  foreach ($taxonomies  as $taxonomy ) {
    echo '<p>' . $taxonomy->name . '</p>';
  }
}
?>

Displaying Custom Taxonomy Items as Tag Cloud

You can create custom taxonomies to be hierarchal or non hierarchal. The hierarchal taxonomies act like categories where you can create a parent item and place child items under it. Non-Heirarchal taxomies act like tags with no way to create a parent or child item. No matter which one you choose you can still display your custom taxonomy in a tag cloud using the wp_tag_cloud function, as shown below:

<?php
wp_tag_cloud( array( 'taxonomy' => 'genre' ) );
?>

Templating Custom Taxonomies

You can create templates for your custom taxonomies as well as items in your custom taxonomy in the following format:

  • taxonomy-{taxonomy}.php -> taxonomy-genre.php
  • taxonomy-{taxonomy}-{taxonomy-item}.php -> taxonomy-genre-drama.php
  • taxonomy.php -> template to handle all custom taxonomies

By default, WordPress will use archive.php for items in the taxonomy as well as the custom taxonomy itself.

Custom Taxonomies to Create a Customized WordPress CMS

Custom taxonomies, along with Custom Post Type and other WordPress features, provide WordPress developers and website owners a complete set of tools and framework to create customized applications based on WordPress. Many WordPress developers are using Taxonomies to create feature rich WordPress solutions, targeting clients with specific needs.

WordPress Ecommerce themes are a fine example of WordPress development taking full advantage of these features. These ecommerce themes use custom taxonomies and custom post types for product listings, grouping items, creating an interface that allows theme users to add products instead of posts. There are also now many “app themes” which make extensive use of post types an taxonomies to create rich CMSs tailored to specific uses.

Ideas

Functionality Plugin ?

Another thing to note, although in our examples we have placed the code in a theme functions.php file – this ties the data down to a specific theme, so if you change themes the data will not be carried over. This may not matter for highly custom websites but if you anticipate the data being required after a change of themes then it makes more sense to place it in a functionality plugin.

Conclusion

In this post we tried to learn and understand how to create custom taxonomies. So far we have covered Custom Post Types and Taxonomies, which are the two main features of WordPress that make it a powerful CMS. It was not the first time we covered custom taxonomies here at WPlift, check out this post which goes over the “Types plugin” so you can create post types and taxonomies in a visual interface. Also, see GenerateWP which is an online generator for all types of custom WordPress code.

I will try to continue this series of posts on using WordPress as a CMS by writing more about advanced topics. In the coming days we will be discussing Custom Fields and Meta Boxes. Please follow @WPlift or subscribe so that you don’t miss these posts.


Noumaan is a blogger and social media expert. He loves Quora, Facebook, Wordpress, OpenSource Software and The Sims.

Leave Yours +

15 Comments

  1. Great post, thank  you for including the cloud part and about templating the custom taxonomies. Sometimes it can be intimidating venturing into this type of stuff. I’m going to try this tutorial on a new project I’m working on. Thanks again.

  2. Nice post – personally I have moved towards using custom fields instead of Taxonomies. I use the Advanced Custom Fields Plugin. It has made my life a lot easier. It’s also easy to code on hte frontend.

    Mark

  3. Seems like an interesting article, however I’m with Irish Web HQ in that I’d rather use custom fields.  That being said I’m sure a time will come where that might be an option, I’ll be glad that I know it.  Thanks for your hard work.

    • Anonymous

      Andrew thanks for the feedback, really appreciate it. However, I don’t think that taxonomies are in any way similar to custom fields. Definitely custom fields can be used to take user input and do different things with the content. But custom taxonomies offer a better way to categorize content, create better UIs, organize and display content more efficiently. 

  4. Jessica Paoli

    This article (and the previous) helped me understand what the post types and taxonomies actually mean! I have followed tutorials to create my own post type and taxonomy, but I didn’t full understand what I was doing except copy and pasting code. Thanks for the in-depth descriptions.

  5. nice tutorial, custom taxonomies are very handy and quick for creating a key value CRUD type thingy.

Leave a Reply

* Required Fields.
Your email will not be published.

Search

Our Sponsors