Custom Taxonomies

First, some terminology. A taxonomy is a group of terms. Terms are individual choices. For example, Post Tags is a taxonomy, and each tag is a term within that taxonomy. Likewise, Post Categories is a taxonomy, and each category is a term. Each taxonomy has a label, a slug, and a description. Individual terms also have labels, slugs, and descriptions. You've seen this before when creating a new category.

Custom taxonomies were introduced in version 2.3, but there was no built-in user interface for them until version 2.8, and very few developers used them. In 2.8, UI support for non-hierarchical taxonomies was added. In 3.0, hierarchical taxonomies are now supported as well, which means you don't have to do the work of creating meta boxes and saving taxonomy terms to the database; WordPress does all that for you. All you have to do is register your new taxonomy and define its labels.

Creating a new taxonomy requires just one function: register_taxonomy(). However, the arguments can get a little complicated, and there's a bit more work you have to do to make permalinks work. Listing 12-1 shows all the possible arguments for a new taxonomy.

Listing 12-1. Registering a new taxonomy register_taxonomy(

'people', //Taxonomy name array('attachment:image', 'attachment:video', 'attachment:audio', 'post', 'page'), //Content object type array(

'rewrite' => array('slug' => 'person'), 'query_var' => true, 'show_ui' => true, 'show_tagcloud' => true, 'hierarchical' => false,

Let's break down these arguments:

• Taxonomy name: an internal name for the new taxonomy.

• Content object type: the types of content that can use this taxonomy. This argument can be a string containing a single content type or it can be an array of several content types, as shown in Listing 12-1.

• Other arguments: an array containing:

• labels: an array containing the name (plural) and the singular name. These provide the text for the administration screens' navigation menu item (beneath Categories and Post Tags) leading to the management page for this taxonomy; and the title for the taxonomy's meta box on the Post ^ Edit screen. (See Figure 12-1.) The singular name ensures that the management page for your taxonomy is grammatically correct. ("Add a new genre" instead of "Add a new genres;" see Figure 12-2.)

• helps: the instructive text that appears below the entry field on the Post ^ Edit screen. Applies only to non-hierarchical taxonomies.

• rewrite: possible values are:

• False: no permalink rules will be created for this taxonomy.

• True: the permalink structure will be /?taxonomy=term in the default structure or /taxonomy/term using any other permalink structure.

• Array containing a slug: the slug will be used in place of the taxonomy name. (See Listing 12-2 for examples of each rewrite method.)

• query_var: possible values are:

False: this taxonomy will not be available in custom queries

• True: you can use the taxonomy name in queries. In this case, query_posts('genre=fantasy'); would return any books tagged with "fantasy" as the genre, as shown in Figure 12-6.

• String: the given string will be the query variable rather than the taxonomy name. (See the Authors taxonomy in Listing 12-2 for the proper syntax.)

• show_ui: whether or not the management screens and meta boxes should be shown for this taxonomy. Defaults to true. You could set this to false if you were creating a hidden taxonomy that would be used in your plugin code, but never by users.

• show_tagcloud: whether this taxonomy should be an option in the tag cloud widget. (See Figure 12-3.) Defaults to the value of show_ui. Applies only to non-hierarchical taxonomies.

• hierarchical: whether this taxonomy should be hierarchical (categorylike) or non-hierarchical (tag-like). Defaults to false (tag-like).

The resulting People taxonomy meta box is shown in Figure 12-1.

Figure 12-1. The People taxonomy

You can register as many new taxonomies as you need. Let's start with some tag-like, non-hierarchical ones.

0 0

Post a comment