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_list_pages(), by default);
  • otherwise nothing.

Usage

 <?php wp_nav_menu($args); ?> 

Default Usage

 <?php $defaults = 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'  => 
);
?>

Parameters

$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
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 ul element which forms the menu
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
Default: true
$fallback_cb
(string) (optional) If the menu doesn't exists, the callback function to use
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
$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
$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

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 assiged to the primary position is 
                 the one used.  If none is assigned, the menu with the lowest ID is used.  */ ?>
	<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>
</div><!-- #access -->

Removing the Navigation Container

<?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' => '' ) ); ?>

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(), wp_nav_menu(), wp_get_nav_menu(), wp_get_nav_menu_item()

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