• Aucun résultat trouvé

Creating a taxonomy navigation template

Dans le document WordPress 2.8 Themes Cookbook (Page 144-148)

Similar to the Archives page template created in Creating an archives page template, a

Taxonomy Navigation page template can be very useful to your site visitors. With the introduction of custom post taxonomies in WordPress 2.8, WordPress users have more options than ever when it comes to classifying their content. A car enthusiast's site may have posts classified by Make, Model, or Transmission Type. Wouldn't it be useful to be able to navigate by those things in addition to the standard post tags and categories?

Custom taxonomies are amazingly powerful and quite easy to put in place.

We'll use a small snippet of code for testing purposes later, but if you want more information on how to use them, see Justin Tadlock's excellent post about custom taxonomies at http://justintadlock.com/

archives/2009/05/06/custom-taxonomies-in-wordpress-28.

In this recipe, you'll learn how to create a page template that allows visitors to browse by any taxonomy that the system has in place. The best part is that you don't need to know ahead of time what taxonomies are available.

Getting ready

To properly use the techniques in this recipe, you'll need to be working with a theme that you previously acquired or developed. If you haven't started developing a custom theme yet, I recommend using the Thematic theme. It can be freely downloaded from the WordPress.org Theme Repository at http://wordpress.org/extend/themes/thematic/.

In addition to properly testing the custom taxonomy navigation for this recipe, we need to add a new taxonomy. Open up your theme's functions.php file and insert the following:

<?php

add_action( 'init', 'wptc_taxonomies' );

function wptc_taxonomies() {

array(

'hierarchical'=>false, 'label'=>'Genres', 'query_var'=>true, 'rewrite' => true )

);

}

This little snippet adds a new taxonomy for Genres, something that might be right at home on a book or movie review site. Go to the WordPress administrative interface and navigate to the Add New Post interface, and then make sure that the new Genres meta box appears. It should look like the following:

Now go ahead and add some genres to a post and publish it, to ensure that there is data to pull for your custom taxonomy.

How to do it...

First, follow the steps in the recipe Creating a simple page template until you reach the point at which you start adding custom content. While following that recipe, modify the filename from hello-world-page-template.php to taxonomies-page-template.php, and change the value of the Template Name: header from Hello World to Taxonomies. Now you're ready to start adding the appropriate content. After the page template comment header, add the following markup to your page template, and then save the file:

<?php get_header(); ?>

<div id="container">

<div id="content">

<?php

$taxonomies = get_object_taxonomies('post');

foreach($taxonomies as $tax) { $obj = get_taxonomy($tax);

?>

<h2><?php echo esc_html($obj->label); ?></h2>

wp_tag_cloud(array(

'number'=>3, 'unit'=>'',

'format'=>'list', 'orderby'=>'count', 'order'=>'DESC', 'taxonomy'=>$tax ));

} ?>

</div>

</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

You can now use your Taxonomies page template. Go and create a new page in the WordPress administrative area, and make sure that it uses the Taxonomies page template. If you need more information on how to do this, see the recipe Creating a simple page template. Visit your newly-created page. You should see output similar to the example shown in the following screenshot:

How it works...

By this time, you should have a pretty good idea of the way in which custom page templates work. If you need a refresher, see the How it works... section of the Creating a simple page template recipe.

Here you're taking advantage of the taxonomy system that has been built into WordPress since Version 2.3, as well as the custom taxonomy capabilities built into WordPress since Version 2.8. The taxonomy system essentially lets you classify objects in your system in ways that makes sense for your particular content. If you're reviewing movies, it makes sense to classify them in Genres. If you're looking at art, it makes sense to classify them by Periods.

After you've classified your content, you need to somehow let visitors navigate according to your custom taxonomies. That is where this recipe comes into play. In this recipe, you use a couple of new functions. First, you take advantage of the get_object_taxonomies function. This function takes a single parameter that indicates the type of object that you want to retrieve taxonomies for, and returns an array of registered taxonomy names for that object type. Next, you iterate over each taxonomy name, retrieve the appropriate taxonomy object, and then display the taxonomy label, and a list of all items in that taxonomy that have been used to classify objects.

The function that you use to display the items in taxonomy is wp_tag_cloud. Most people don't realize the full potential of this function, believing that it is only used for displaying post tags. However, you can use wp_tag_cloud to display items from any taxonomy, by passing a taxonomy name in as a parameter.

You pass other parameters as well, and it is important to know why you provide the values that you do:

number—used to limit the number of taxonomy items present in the list output unit—setting this parameter to an empty string ensures that all items are the same size

format—setting this parameter to the value list causes the output to be an unordered list

orderby—you can use different values here, but using count ensures that your taxonomy items are sorted by the number of objects they are assigned to

order—setting this to DESC makes the taxonomy items order themselves from high to low

taxonomy—the value here determines which object classification will be looked at inside of the function

Internal to wp_tag_cloud is a complicated SQL query that looks at different taxonomy tables and the posts table, applies the options that you pass, and generates the appropriate output.

See also

Creating an archives page template Creating a simple page template

Dans le document WordPress 2.8 Themes Cookbook (Page 144-148)