Gordonmac Dot Com

Mostly a web development blog

WordPress – Custom taxonomy navigation menus

Posted: June 22nd, 2012 | Tags: | Posted in: Tutorials, Wordpress
Note: This tutorial was originally published in 2012. The tips and techniques explained may be outdated.

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.