Along with WordPress 3.0 we now have access to the really useful wp_nav_menu functionality. We can now create our own menus without resulting to several plugins or tricks.
Another cool thing we can do with this new menu is build the sitemap. I was in need the other day of a plugin that would generate a sitemap page (something like an archive page). Not an xml sitemap, just a standard page from where the visitors of the site could navigate more easily. The best solution appeared to be an WordPress menu shortcode.
Since I couldn’t find something like this I realized that I could use the wp_nav_menu function and built a shortcode to insert it into my page.
wp_nav_menu shortcode
To install the shortcode just place this code inside functions.php file of your theme.
// 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 the shortcode just place [listmenu menu=Sitemap] into your post and that’s it (replace Sitemap with the id, slug or name of the menu you want to list.)
You can also use all the variables that come with the new wp_nav_menu function, like menu_class, or container so you can customize your menu really easy. Just separate the attributes by space like so:
[listmenu menu=Sitemap menu_class=sitemap_menu]
Conclusions
This is really useful for client sites, where you want to make it easy edit and modify the site without braking the html. We could have just manually created the sitemap, but this is a lot nicer. Another way you could use this shortcode would be to create sub-menus on certain pages only.
If you find use for this shortcode in an interesting way don’t hesitate to share it in the comments.
No related posts.







One Comment
BRILLIANT. Thank you SO much for working this out!
One Trackback
[...] wp_nav_menu shortcode Another cool thing we can do with this new menu is build the sitemap. I was in need the other day of a plugin that would generate a sitemap page (something like an archive page). (tags: wordpress menu tutorial sitemap) Leave a Reply Click here to cancel reply. [...]