Codex

Function Reference/wp nav menu

Contents

Description

Displays a navigation menu created in the AppearanceMenus panel.

Given a theme_location parameter, the function displays the menu assigned to that location, or nothing if no such location exists or no menu is assigned to it.

If not given a theme_location parameter, the function displays

  • the menu matching the ID, slug, or name given by the menu parameter, if that menu has at least 1 item;
  • otherwise, the first non-empty menu;
  • otherwise, output of the function given by the fallback_cb parameter (wp_page_menu(), by default);
  • otherwise nothing.

Usage

 <?php wp_nav_menu$args ); ?> 

Default Usage

 <?php $defaults = array(
  
'theme_location'  => ,
  
'menu'            => 

  
'container'       => 'div'
  
'container_class' => 'menu-{menu slug}-container'
  
'container_id'    => ,
  
'menu_class'      => 'menu'
  
'menu_id'         => 
,
  
'echo'            => true,
  
'fallback_cb'     => 'wp_page_menu',
  
'before'          => ,
  
'after'           => 
,
  
'link_before'     => ,
  
'link_after'      => 
,
  
'items_wrap'      => '<ul id=\"%1$s\" class=\"%2$s\">%3$s</ul>',
  
'depth'           => 0,
  
'walker'          => );
?>

Parameters

$theme_location
(string) (optional) the location in the theme to be used--must be registered with register_nav_menu() in order to be selectable by the user
Default: None
$menu
(string) (optional) The menu that is desired; accepts (matching in order) id, slug, name
Default: None
$container
(string) (optional) Whether to wrap the ul, and what to wrap it with. Allowed tags are div and nav. Use false for no container e.g. container => false
Default: div
$container_class
(string) (optional) the class that is applied to the container
Default: menu-{menu slug}-container
$container_id
(string) (optional) The ID that is applied to the container
Default: None
$menu_class
(string) (optional) CSS class to use for the containing div element which forms the default menu, or the ul element when a custom menu is configured in the admin interface
Default: menu
$menu_id
(string) (optional) The ID that is applied to the ul element which forms the menu
Default: menu slug, incremented
$echo
(boolean) (optional) Whether to echo the menu or return it. For returning menu use '0'
Default: true
$fallback_cb
(string) (optional) If the menu doesn't exist, the fallback function to use. Set to false for no fallback.

Note: passes $args to the custom function.

Default: wp_page_menu
$before
(string) (optional) Output text before the <a> of the link
Default: None
$after
(string) (optional) Output text after the </a> of the link
Default: None
$link_before
(string) (optional) Output text before the link text
Default: None
$link_after
(string) (optional) Output text after the link text
Default: None
$items_wrap
(string) (optional) Whatever to wrap the items with an ul, and how to wrap them with
Default: None
$depth
(integer) (optional) how many levels of the hierarchy are to be included where 0 means all
Default: 0
$walker
(object) (optional) Custom walker object to use (Note: You must pass an actual object to use, not a string)
Default: new Walker_Nav_Menu

Examples

Default example

<div class="access">
  <?php wp_nav_menu(); ?>
</div>

Targeting a specific Menu

<?php wp_nav_menu( array('menu' => 'Project Nav' )); ?>

Used in the Twenty Ten theme

<div id="access" role="navigation">

    <?php /*

    Allow screen readers / text browsers to skip the navigation menu and
    get right to the good stuff. */ ?>

    <div class="skip-link screen-reader-text">
        <a href="#content" title="<?php esc_attr_e( 'Skip to content', 'twentyten' ); ?>">
        <?php _e( 'Skip to content', 'twentyten' ); ?></a>
    </div>

    <?php /*

    Our navigation menu.  If one isn't filled out, wp_nav_menu falls
    back to wp_page_menu.  The menu assigned to the primary position is
    the one used.  If none is assigned, the menu with the lowest ID is
    used. */

    wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>

</div><!-- #access -->

Removing the Navigation Container

In order to remove navigation container, theme location specified in functions.php and used among arguments in function wp_nav_menu ( eg. 'theme_location' => 'primary-menu' ) must have a menu assigned to it in administration! Othervise argument 'container' => 'false' is ignored.

<?php
function my_wp_nav_menu_args( $args = '' )
{
	$args['container'] = false;
	return $args;
} // function

add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
?>

OR

<?php wp_nav_menu( array( 'container' => '' ) ); ?>

Removing the ul wrap

This example will remove the ul around the list items.

<?php wp_nav_menu( array( 'items_wrap' => '%3$s' ) ); ?>

Adding a Word at the Beginning of the Menu

This example will allow you to add the word of your choice to the beginning of your menu as a list item. In this example, the word "Menu:" is added at the beginning. You may want to set an id on the list item ("item-id" in this example) so that you can use CSS to style it.

<?php wp_nav_menu( array( 'theme_location' => 'primary', 'items_wrap' => '<ul><li id="item-id">Menu: </li>%3$s</ul>' ) ); ?>

Adding Conditional Classes to Menu Items

This example would let you add a custom class to a menu item based on the condition you specify. Don't forget to change the condition.

<?php
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
     if(is_single() && $item->title == "Blog"){ //Notice you can change the conditional from is_single() and $item->title
             $classes[] = "special-class";
     }
     return $classes;
}
?>

I was trying to customize the look of a specific menu item: Blog on single post pages. After rethinking the code above, it is much simpler to use the body class .single if you can. In my case it works. But nonetheless, the above code is really handy.

Different menus for logged-in users

This example would cause a menu to show for logged-in users and a different menu for users not logged-in.

<?php
if ( is_user_logged_in() ) {
     wp_nav_menu( array( 'theme_location' => 'logged-in-menu' ) );
} else {
     wp_nav_menu( array( 'theme_location' => 'logged-out-menu' ) );
}
?>

Change log

Source file

wp_nav_menu() is located in wp-includes/nav-menu-template.php.

Resources

Related

Navigation Menu: register_nav_menus(), register_nav_menu(), unregister_nav_menu(), has_nav_menu(), wp_nav_menu(), wp_get_nav_menu_items()

See also index of Function Reference and index of Template Tags.