Extending Custom Menus

Custom Menus in WordPress 3 are great. They allow for easy drag and drop to create custom menu structures. Custom menus can be found by going to Appearance > Menus

The current default template, Twenty Ten, only uses one custom menu. Here we’re going to do two things – Firstly we’re going to extend the number of menu’s you can use and secondly we’re going to create some shortcode so we can drop our menu’s right into a post, page or sidebar widget.

Open up the functions.php file in your editor and go to the register_nav_menus array around line 99

register_nav_menus( array(
 'primary' => __( 'Primary Navigation', 'twentyten' ),
 ) );

Now add additional menus into the array

register_nav_menus( array(
 'primary' => __( 'Primary Navigation', 'twentyten' ),
 'menu-one' => __( 'Menu One', 'twentyten' ),
 'menu-two' => __( 'Menu Two', 'twentyten' ),
 'menu-three' => __( 'Menu Three', 'twentyten' ),
 ) );

That’s it – We now have our additional menu’s

The next stage is to call on these new menu’s. Traditionally that means embedding code within our template like so –

<?php wp_nav_menu(  array( 'theme_location' => 'menu-one' ));  ?>

Instead we’re going to further add to our functions.php file and convert this into shortcode that can be used anywhere in WordPress and I found a great example of how to do this here – http://www.cozmoslabs.com/2010/06/28/wp_nav_menu-shortcode/

// Function that will return our WordPress menu
function list_menu($atts, $content = null) {
	extract(shortcode_atts(array(
		'menu'            => '',
		'container'       => 'div',
		'container_class' => '',
		'container_id'    => '',
		'menu_class'      => 'menu',
		'menu_id'         => '',
		'echo'            => true,
		'fallback_cb'     => 'wp_page_menu',
		'before'          => '',
		'after'           => '',
		'link_before'     => '',
		'link_after'      => '',
		'depth'           => 0,
		'walker'          => '',
		'theme_location'  => ''),
		$atts));

	return wp_nav_menu( array(
		'menu'            => $menu,
		'container'       => $container,
		'container_class' => $container_class,
		'container_id'    => $container_id,
		'menu_class'      => $menu_class,
		'menu_id'         => $menu_id,
		'echo'            => false,
		'fallback_cb'     => $fallback_cb,
		'before'          => $before,
		'after'           => $after,
		'link_before'     => $link_before,
		'link_after'      => $link_after,
		'depth'           => $depth,
		'walker'          => $walker,
		'theme_location'  => $theme_location));
}
//Create the shortcode
add_shortcode("listmenu", "list_menu");

To use this shortcode within a post


This works particularly well in conjunction with the side content plugin, allowing custom indexes for child pages within the sidebar.

This entry was posted in Technical and tagged , . Bookmark the permalink.

2 Responses to Extending Custom Menus

  1. Pingback: Shortcode in custom sidebar widget | Seja Design – WordPress Templates