Using Custom Taxonomies

If you publish the Harry Potter post shown in Figure 12-2, you can then browse to your archive page using the permalink structure you created and one of the terms you assigned. In this case, /genre/fantasy results in the archive page shown in Figure 12-7.

Word Press

Harry Folter anil llie Sorcerer's Stone Harry Folter and llie Chamber of Seems Harry Potter and the Prisoner of Ajinhan Harry Potter and the Goblet of Fire Harry Potter and the Order of the Phoenix Harry Potter and the Half Blood Prinac Harry Potter and the — Continue icodni r >s '-o ,

Figure 12-7. The fantasy genre archive page

As you can see, this archive page is a little generic. The Twenty Ten theme does not include a taxonomy archive template, so the title of the page is simply "Blog Archives." In order to fix it up, you can either add a conditional statement checking is_tax() to your generic archive.php template, or you can create a taxonomy.php template. Let's use the latter option. Listing 12-4 shows a basic taxonomy template for Twenty Ten.

Listing 12-4. A new taxonomy.php file for the Twenty Ten theme

<div id="container">

<div id="content">

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var(

echo ucwords($term->taxonomy) . ': ' . $term->name;

<?php get_generic_template( 'loop', 'archive' ); ?>

</div><!-- #content -- > </div><!-- #container -- >

<?php get_sidebar(); ?> <?php get_footer(); ?>

All that's changed from archive.php is that you've removed the date-based conditionals and replaced the standard title with a bit of code that fetches the current taxonomy and term. Here, you've added PHP's ucwords() function to make sure the taxonomy name is uppercase, no matter how the user entered it. This changes the archive title in Figure 12-6 to "Genre: Fantasy." That's still rather lackluster; you can do better.

Listing 12-5 shows a revised heading tag. You can put this in a taxonomy-genre.php file, where you know your grammar will be correct for any of the genre terms.

Listing 12-5. The <h1> heading in taxonomy-genre.php

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo 'Posts in the '.$term->name.' '.$term->taxonomy;

This gives provides the "Posts in the Fantasy genre" which is much better.

It would also be nice to include the taxonomies in the post's meta data alongside the categories and post tags. You could use the_taxonomies(), but this function is indiscriminate and prints all the taxonomies, including the built-in ones. There's no way to limit it to a particular taxonomy. Its output for the Harry Potter post is shown in Listing 12-6.

Listing 12-6. Result of the_taxonomies()

Categories: Main. Post Tags: boarding schools, magic, and wizards. Genres: Fantasy and Young Adult. Authors: J. K. Rowling. Completion Status: completed.

To list just one taxonomy at a time with better control over the output, add them to the template individually, using get_the_term_list() as shown in Listing 12-7. This function won't print anything if there are no terms associated with the post, so you don't need to check first to see if it's empty.

Listing 12-7. Listing the terms of a particular taxonomy alongside categories and tags

<p>Posted in <?php the_category(',') ?>

<?php echo get_the_term_list( $post->ID, 'series', 'in the series ', ', ', '' ); ?></p> <?php if ( the_tags('<p>Tagged with ', ', ', '</p>') ) ?>

WordPress doesn't provide an archive page for a taxonomy as a whole. That is, there's no page automatically built that will list all the terms used in a particular taxonomy; you have to build one yourself. Listing 12-8 shows how to create a linked list of all the terms in a particular taxonomy.

Listing 12-8. Listing taxonomy terms

$authors = get_terms('authors'); foreach ($authors as $author) { ?>

<li><a href="<?php echo get_term_link( $author, 'authors') ?>"><?php echo $author->name; ?></a> <span class="comments"><?php echo $author->description; ?></span> </li> <?php } ?> </ul>

You could turn that into a widget or just put it into a sidebar. For the purpose of demonstration, however, let's add a page template containing this code in place of the content. The result is shown in Figure 12-8.

Figure 12-8. The author term list

There's also no built-in way to display all the taxonomies. The get_taxonomies() function will display them, but it includes all the registered taxonomies, including the defaults, as shown in Listing 12-9.

Listing 12-9. Listing all registered taxonomies

foreach ($taxes as $tax) echo '<li>'.$tax.'</li>';

If you're using the series books taxonomies, the list will be:

• link_category

To remove the built-in taxonomies from the list and show only the custom taxonomies you've created, use the taxonomies' _builtin argument. It's a private argument intended for WordPress's internal use, but since you're not abusing it to create your own "built-in" taxonomy, you can use it to filter your taxonomy list, as shown in Listing 12-10.

Listing 12-10. Listing only custom taxonomies

$taxes = get_taxonomies(array('_builtin' => false)); foreach ($taxes as $tax) echo '<li>'.$tax.'</li>';

Now the result is more useful:

• author status

Other Taxonomy Examples

The Taxes plugin ( was written a few years ago to demonstrate the then-new taxonomy features. It provides a way to tag all your content— posts, pages, and media uploads—with people's names, much like the tagging features on Flickr and Facebook. Figure 12-9 shows what the media uploader looks like with the People taxonomy included.

Figure 12-9. People tagging in the media uploader

The Series plugin is another great example. It's used to organize posts that are part of an ongoing series, as shown in Figure 12-10.

Figure 12-10. A list of posts in the Series plugin

Here you can see why you might want to create taxonomy-specific archive templates. Your site might not be as mixed up as the demo, but here's the genre tag cloud appearing on the tutorial series archive page. If you don't really need to create a whole new template file, you might use conditional tags to include different sidebars for each.

0 0

Post a comment