Create Custom Taxonomies In WordPress

WordPress 3 introduced custom taxonomies as a core feature. The easy-to-use custom taxonomies function gives site designers some powerful tools for building a good information architecture. Learn what taxonomies are, why they’re useful, and how to use them in today’s tutorial!

What is a Taxonomy?

Taxonomies are different methods for classifying things. WordPress’ custom taxonomies make it possible to structure large amounts of content in a logical, well-organized way. In WordPress, categories are set up as a hierarchal taxonomy, and tags are set up as a multifaceted taxonomy.

Taxonomy content can be displayed in a theme using taxonomy templates. Within a template, there are sample ways to display your data with built-in taxonomy functions.

Create a Custom Taxonomy

In WordPress, you can create (or “register”) a new taxonomy by using the register_taxonomy() function. Each taxonomy option is documented in detail in the WordPress Codex. All you have to do is to copy the code below, in your functions.php.

/**
 * Add custom taxonomies
 *
 * Additional custom taxonomies can be defined here
 * http://codex.wordpress.org/Function_Reference/register_taxonomy
 */
function add_custom_taxonomies() {
	// Add new "Color" taxonomy to Posts
	register_taxonomy('color', 'post', array(
		// Hierarchical taxonomy (like categories)
		'hierarchical' => true,
		// This array of options controls the labels displayed in the WordPress Admin UI
		'labels' => array(
			'name' => _x( 'Color', 'taxonomy general name' ),
			'singular_name' => _x( 'Color', 'taxonomy singular name' ),
			'search_items' =>  __( 'Search Color' ),
			'all_items' => __( 'All Color' ),
			'parent_item' => __( 'Parent Color' ),
			'parent_item_colon' => __( 'Parent Color:' ),
			'edit_item' => __( 'Edit Color' ),
			'update_item' => __( 'Update Color' ),
			'add_new_item' => __( 'Add New Color' ),
			'new_item_name' => __( 'New Color Name' ),
			'menu_name' => __( 'Color' ),
		),
		// Control the slugs used for this taxonomy
		'rewrite' => array(
			'slug' => 'color', // This controls the base slug that will display before each term
			'with_front' => false, // Don't display the category base before "/color/"
			'hierarchical' => true // This will allow URL's like "/color/red/jacket/"
		),
	));
}
add_action( 'init', 'add_custom_taxonomies', 0 );

After adding this to your theme’s functions.php file, you should see a new taxonomy under the “Posts” menu in the admin sidebar. It works just like categories but is separate and independent.

Retrieve Taxonomy Information About the Current Post

$color_list = get_the_term_list( $post->ID, ‘color’, ‘<strong>Color:</strong> ‘, ‘, ‘, ” );

Display a List of Taxonomy Terms

/**
 * Create an unordered list of links to active color archives
 */
$color_list = wp_list_categories( array(
  'taxonomy' => 'color',
  'orderby' => 'name',
  'show_count' => 0,
  'pad_counts' => 0,
  'hierarchical' => 1,
  'echo' => 0,
  'title_li' => 'Color'
) );

// Make sure there are terms with articles
if ( $color_list )
	echo '<ul>' . $color_list . '</ul>';

Create a Taxonomy Tag Cloud

The wp_tag_cloud() function makes creating a tag cloud with a custom taxonomy easy.

// Color tag cloud
<?php
$color_cloud = wp_tag_cloud( array(
	'taxonomy' => 'color',
	'echo' => 0
) );

// Make sure there are terms with articles
if ( $color_cloud ): ?>
<h2>Posts by Color</h2>
<div>
	<?php echo $color_cloud; ?>
</div>
<?php endif; ?>

Get All Terms in a Taxonomy

the get_terms() function can be quite handy for this.

<?php
// Get a list of all terms in a taxonomy
$terms = get_terms( "color", array(
	'hide_empty' => 0,
) );
$color = array();
if ( count($terms) > 0 ):
	foreach ( $terms as $term )
		$color[] = $term->name;

	$color_str = implode(', ', $color);
?>
<h2>Color Variation</h2>
<p>We have different variation of colors for this product like <?php echo $color_str; ?> and more.</p>
<?php endif; ?>

This will output the following HTML:
<h2>Color Variation</h2>
<p>We have different variation of colors for this product like red, yellow, green and more.</p>

WP_Query and tax_query

The WP_Query class enables you to create a custom loop. WordPress 3.1 introduced a new parameter for the class called tax_query, which allows you to display content from a taxonomy in many unique ways.

<?php
/**
 * Display a list of the most recent post of red color
 *
 * @class WP_Query http://codex.wordpress.org/Class_Reference/WP_Query
 */
$color_query = new WP_Query( array(
	'post_type' => 'post',
	'posts_per_page' => 10,
	'tax_query' => array(
		array(
			'taxonomy' => 'color',
			'field' => 'slug',
			'terms' => 'red'
		)
	)
) );
// Display the custom loop
if ( $color_query->have_posts() ): ?>
<h2>Latest post of red color</h2>
<ul>
	<?php while ( $color_query->have_posts() ) : $color_query->the_post(); ?>
	<li><span><?php the_time(get_option('date_format')); ?></span> – <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
	<?php endwhile; wp_reset_postdata(); ?>
</ul><!--// end .postlist -->
<?php endif; ?>

We could easily make this set-up dynamic by using the get_term_by() or get_terms() functions.

Conclusion

I hope this tutorial explained clearly what taxonomies are and showed you how to make use of them in WordPress 3 as a powerful organizational tool.

Leave a Reply

Your email address will not be published. Required fields are marked *