The product of today’s messing around with WordPress custom taxonomies in a bid to finish a client website - I needed to  generate a navigation based on categories stored in a custom taxonomy.

Here’s the gist of it:

function displayTaxonomyNav($taxonomy, $baseURL)
{
	$output = NULL;
	$rangeNames = get_terms($taxonomy, 'orderby=id&hide_empty=0');

	foreach($rangeNames as $range)
	{
		$output .= '<li><a href="' . $baseURL  . $range->slug . '/">' . $range->name . '</a></li>'."\n";
	}

	return $output;
}

When you call the function in your template just change $taxonomy to the name of the custom taxonomy you registered, and the $baseURL to the base page to display the items within the categories:

<ul id="navigation-product-range">
    <?php echo displayTaxonomyNav('product-range', '/products/range/'); ?>
</ul>

That’s all there is to it really.

… by . Last updated on

Comment via Twitter

Interesting…