Nonhierarchical Taxonomies

Non-hierarchical taxonomies are just like tags. They don't have parent/child relationships. The meta box added to the Post ^ Edit screen looks like the tag entry field: a single text input box with an autocomplete feature.

To illustrate, let's create the taxonomies for a hypothetical site listing books that are part of a series. For this project, you want separate taxonomies for the genre, the series status, and the author(s) of the books (as opposed to the post author). Keep the regular post tag field—use it for general keywords associated with the books.

Listing 12-2 shows the new taxonomies you'll use to create the series books site. Since there are three of them, wrap them into a single function, which is called using the init() action hook. (This is exactly how the built-in taxonomies are created.)

In addition to creating the taxonomies themselves, you must flush the rewrite rules (which are cached) in order for the new rewrite rules to take effect. Since this needs to be done just once, wrap it up in a plugin activation function. This function calls create_book_series_tax() once to get all the new rewrite rules into place, then flushes the rewrite rules. This forces the list of rewrite rules to be rebuilt, and the new ones will be included.

Listing 12-2. Creating the series book taxonomies

Plugin Name: Series Book Taxonomies

add_action('init', 'create_book_series_tax'); register_activation_hook( _FILE_, 'activate_book_series_tax' );

function activate_book_series_tax() { create_book_series_tax(); $GLOBALS['wp_rewrite']->flush_rules();

function create_book_series_tax() { register_taxonomy( 'genre', 'post', array(

register_taxonomy( ' author' , array('attachment:image', 'attachment:video', 'attachment:audio', 'post',

register_taxonomy( 'status', 'post', array(

'helps' => __('Enter the series status (completed, ongoing, etc.).'),

Here, you omit all the arguments that would be set to their default values (rewrite, query_var, show_ui, show_tagcloud, and hierarchical) except on the Authors taxonomy, where I've demonstrated how you would change the rewrite and query slugs if you wanted to. I also used the Authors taxonomy to show how you could add a taxonomy to more than one type of content. In this case, it's possible that your site will include photos of an author, or even a video or sound file. You've added all the possible content types. (Custom taxonomies don't work for links.)

Figure 12-2 shows the resulting meta boxes on the Post ^ Edit screen. Figure 12-3 shows the management page for the status taxonomy, and Figure 12-4 shows the tag cloud widget options once all three taxonomies have been added.

Figure 12-2. The resulting taxonomy boxes, on the right
TTw i*itrrct>cf ti nor pnmftmr 6* iVM. ttowwr, jovm* itompi may ftmr ft.

Figure 12-3. The management page for the Completion Status taxonomy

Figure 12-4. The tag cloud widget with the new taxonomy options

Was this article helpful?

0 0

Post a comment