Dynamically modify WordPress menus (3)

Part 3. Auto-generating a submenu for a taxonomy

This is the third article in a series that shows how you can dynamically change a WordPress menu, depending on runtime data. 

  • In Part 1. Introduction, I discussed a simple example that shows how you can add dynamic data to a menu item.
  • In Part 2. Conditional submenus I showed how to replace an entire submenu depending on whether the user is logged in.
  • In this Part 3, I will show how to automatically generate a submenu that contains all the terms of taxonomy.

What is a taxonomy?

A taxonomy is a classification scheme that allows you to categorize posts according to some characteristics they have in common. You are probably familiar with the two standard taxonomies in WordPress: categories and tags. The blog you are currently reading has four categories of posts: WordPress, Programming, Website Management and Other. Incidentally, these categories are items in this website’s menu, so that you can easily find all posts on a given topic that interests you.

WordPress also allows you to create additional, “custom” taxonomies. This is often used by plugin authors to add specific functionality. For instance, it could be used to distinguish between types of products in the e-commerce section of a website. So, in addition to different categories of posts, the site may contain different types of products. Since the product_type classification has nothing in common with the post category, it makes sense to define product_type as a separate taxonomy.

Example: Printables for sale

Let’s take the example of a site that sells printable worksheets for use in education. There are multiple types of printables: colouring pages, crossword puzzles, quizzes, etc.

Under the Printables menu item, we want to show a list of all the printable types, so that customers can easily narrow their search to the type of worksheet they are looking for.

But as the website owner adds more printables, the number of printable types is likely to expand as well. Wouldn’t it be nice if the Printables submenu automatically reflected all the new types?

Auto-generating a submenu

As in the previous articles in this series, the menu is generated in the wp_nav_menu_objects filter.

This time, the code checks the menu item’s title to know where it should generate the submenu. We could just as well have used a pseudo CSS class to identify the main menu item, as in Part 2.

The taxonomy_submenu() function gets the list of taxonomy terms in alphabetical order, and creates one submenu item for each.

add_filter( 'wp_nav_menu_objects', 'nav_menu_filter', 10, 2 );
function nav_menu_filter( $menu_items, $args )  {
	$pt_submenu = array();
	foreach ($menu_items as $menu_item) {
		// Add submenu with all the printable types
		if ($menu_item->title == 'Printables') {
			$pt_submenu = taxonomy_submenu( $menu_item->ID, 'printable_type' );
	$menu_items = array_merge( $menu_items, $pt_submenu );
	return $menu_items;

// Returns array of menu items, one for each term in the taxonomy
function taxonomy_submenu( $parent_id, $taxonomy ) {
	$submenu_items = array();
	$taxonomy_terms = get_terms( array( 'taxonomy' => $taxonomy, 'orderby' => 'name' ) );
	foreach($taxonomy_terms as $t){
		$t->ID = $t->term_id;
		$t->menu_item_parent = $parent_id;
		$t->title = $t->name;
		$t->url = get_term_link( $t, $taxonomy );		
		$submenu_items[] = clone($t);	
	return $submenu_items;

To auto-generate a submenu of categories, use ‘category’ as the taxonomy name. For tags, use ‘post_tag’.

Leave a Reply

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