Using Your Custom Taxonomy
Now that you've created your custom taxonomy you need to know how to use it on your web site. As always, WordPress features some very easy-to-use functions for working with your custom taxonomy. The following shows how you can display a tag cloud showing your custom taxonomy terms:
<?php wp_tag_cloud( array( 'taxonomy' => 'ingredients', 'number' => 5 ) ); ?>
The wp_tag_cloud function can accept a lot of different arguments, but in this example you're only using two: taxonomy and number. First, you set your taxonomy to ingredients; this tells WordPress to only return taxonomy terms defined under the custom taxonomy you created for ingredients. Next you define the number of terms you want to display, which in this example is 5. Calling this function in your theme sidebar displays a nice tag cloud that shows the five taxonomy terms with the most posts assigned to them.
You can also use query_posts to display posts for a specific taxonomy term. Say you want to create a custom loop to only display recipes that use nutmeg:
<?php query_posts( array( 'ingredients' => 'nutmeg', 'showposts' => 15 ) ); ?>
That's it! The two query_posts arguments you send are the taxonomy name, ingredients in this case, and the number of posts to display. Remember that you can pass more query_posts arguments to further define what posts are displayed and in what order.
You can also easily display custom taxonomy terms assigned to each post. To do this you'll be using the get_the_term_list WordPress function. This function works very similarly to get_the_tag_list, but is for building a custom taxonomy term list instead.
<?php echo get_the_term_list( $post->ID, 'ingredients', 'Ingredients Used: ', ', ', '' ); ?>
The preceding code displays all custom taxonomy terms assigned to the post you are viewing. This code does need to be in The Loop in your theme template file to work correctly. To execute the function you send in the post ID, custom taxonomy name, and the title you want displayed next to the terms. Remember, you can always visit the function reference to learn more about this function and what parameters are allowed: http: //codex.wordpress.org/Function_Reference/get_the_term_list.
The get_terms function can also be used to retrieve an array of your custom taxonomy values. In the following example you retrieve all of the terms for your ingredients taxonomy and loop through the values displaying the term name:
$terms = get_terms('ingredients'); foreach ($terms as $term) {
Keep in mind that you need to make sure the taxonomy is defined before you start working with custom taxonomy values. If any of the preceding examples return blank, that means they are being executed before your register_taxonomy function is being called to define your custom taxonomy.
Defining custom taxonomies in WordPress is a very powerful way to organize your web site content. Using the preceding methods can help transform your web site into a content management system using the power of WordPress.
Post a comment