WordPress Tricks: Customizing the output of “wp_nav_menu()”

There are times when I need to output a menu in WordPress with very custom settings – for example, with no containing UL, with a specific class on the wrapping UL, with the links wrapped in specific tags (spans, for example), etc. Here is the call to a WordPress menu that I use and customize to output the menu exactly as I need it:

<?php wp_nav_menu( 
array ( 
'menu' => 'Main Menu', //the name of the menu you are trying to output
'container' =>false, //set to UL or DIV, or 'false' for no wrapper
'container_class' => '',//the class that is applied to the container
'container_id'    => '',//the id that is applied to the container
'before' => '<div>', //what the a tags themselves are wrapped in
'after' => '</div>', //what the a tags themselves are wrapped in 
'link_before' => '<span>', //what the words within the link are wrapped in 
'link_after' => '</span>', //what the words within the link are wrapped in
'items_wrap' => '%3$s', //what the LI elements are wrapped in - "%3$s" is the LI list itself. To wrap the LI list in a div, you would use "<div class="YOURCLASS">%3$s</div>"
'depth' => 0, ) ); ?>

An article which explains the “items_wrap” argument excellently is http://wordpress.stackexchange.com/questions/19245/any-docs-for-wp-nav-menus-items-wrap-argument so be sure to check that out if you are looking for more information.

And of course, for information on all of the parameters that you can use in wp_nav_menu, you will want to check out the official documentation on WordPress – http://codex.wordpress.org/Function_Reference/wp_nav_menu

Meet the author

Libby Fisher is an experienced freelance web developer, recently relocated from Seattle to Boston, and passionate about developing websites that are both aesthetically appealing and intuitively usable - or as she prefers: "beautifully effective."