WordPress Taxonomies

Wordpress TaxonomiesHave you found that you have thousands of tags that are unmanageable? Have you dreamed you could somehow start over?

Well today you can thanks to custom WordPress Taxonomies. Taxon-what? A Taxonomy is just a fancy word that represents a way you can better sort your posts, pages and links in WordPress.

In this WordPress Video Tutorial you’ll find out everything you ever wanted to know about WordPress Taxonomies and more.

Wordpress TaxonomyHere I’ll create a custom Taxonomy that will contain only tags that would commonly be used as ingredients for a recipe.

After you create this new taxonomy it will show up just like Categories and Tags do on your dashboard. You can then create custom tags of your choosing also in the same ways as Categories and Tags.

The interface is also identical as you see here.

Ingredient Taxonomy

Creating WordPress Taxonomies

You might think that it must be terribly hard to create custom WordPress Taxonomies, but like most things in WordPress it is very simple.

Just open the functions.php file that is in your WordPress Theme and type the following at the bottom of the file.

add_action( ‘init’, ‘create_ingredient_taxonomy’, 0);

function create_ingredient_taxonomy()
{
register_taxonomy ( ‘ingredients’, ‘post’, array( ‘hierarchical’ => true, ‘label’ => ‘Ingredients’, ‘query_var’ => true, ‘rewrite’ => true ) );
}

The add_action() function tells WordPress to run the function named create_ingredient_taxonomy as soon as WordPress has been initialized or loaded.

The 0 attribute tells WordPress that this action is of the highest priority. The larger that number is, the less important it is.

I’ll cover this subject in more detail when I teach you to create custom plugins and widgets.

Then when the function create_ingredient_taxonomy is called the following function is called:

register_taxonomy ( ‘ingredients’, ‘post’, array( ‘hierarchical’ => true, ‘label’ => ‘Ingredients’, ‘query_var’ => true, ‘rewrite’ => true ) );

Here is what it means:

  • ‘ingredients’ : Will be the name WordPress will use to identify your taxonomy data in the WordPress Database
  • ‘post’ : Signifies that you want to use this taxonomy in your posts. You could instead use page or link.
  • ‘hierarchical’ : Signifies that you want to be able to create parent and child elements like you do with Categories. If set to false they will instead act like Tags.
  • ‘label’ : Defines the name for the Taxonomies on your site and in the WordPress Admin
  • ‘query_var’ : If true will allow you to search for posts based off of the taxonomies assigned
  • ‘rewrite’ : Will use informative urls when viewing a page if set to true

That’s all you need to do to add this nice feature to your WordPress website.

WordPress Tag Clouds and Taxonomies

In the above video I also talked about the code needed to display tag clouds of your taxonomies in WordPress. Here it is

<?php wp_tag_cloud( array (‘taxonomy’ => ‘ingredients’, ‘smallest’ => 7, ‘largest’ => 20, ‘unit’ => ‘pt’, ‘number’ => 40 )); ?>

The above code will call and display a tag cloud using your custom taxonomy.

  • taxonomy : Will be the name of your custom taxonomy
  • smallest : Defines the size of the font for the least used taxonomy
  • largest : Defines the size of the font for the most used taxonomy
  • unit : Defines the unit of size for the fonts
  • number : Defines the maximum number of taxonomies to display

I hope that helps you to better organize your data inside of WordPress.

I’ll be covering plugins and widgets very soon. Leave any questions or comments below.

Till Next Time

Think Tank

2 Responses to “WordPress Taxonomies”

  1. Marie Glahn says:

    Thanks a lot for your brilliant tutorials on building wordpress themes. I feel really capable now to try it out myself.

Leave a Reply

Your email address will not be published.

Google+