This page redirects to an external site: https://developer.wordpress.org/reference/functions/wp_nav_menu/
Languages: English • 日本語 Português do Brasil • (Add your language)
Displays a navigation menu created in the Appearance → Menus panel.
Given a theme_location parameter, the function displays the menu assigned to that location. If no such location exists or no menu is assigned to it, the parameter fallback_cb will determine what is displayed.
If not given a theme_location parameter, the function displays
Note: As of 3.5, if there are no menu items, no HTML markup will be output.
<?php wp_nav_menu( $args ); ?>
<?php $defaults = array( 'theme_location' => '', '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' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 'depth' => 0, 'walker' => '' ); wp_nav_menu( $defaults ); ?>
new Walker_Nav_MenuShows the first non-empty menu or wp_page_menu().
<div class="access"> <?php wp_nav_menu(); ?> </div>
<?php wp_nav_menu( array('menu' => 'Project Nav' )); ?>
In the case that no menu matching menu is found, it seems that passing a bogus theme_location is the only way to prevent falling back to the first non-empty menu:
<?php
wp_nav_menu(
array(
'menu' => 'Project Nav',
// do not fall back to first non-empty menu
'theme_location' => '__no_such_location',
// do not fall back to wp_page_menu()
'fallback_cb' => false
)
);
?>
<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 -->
In order to remove the 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! Otherwise, the argument 'container' => 'false' is ignored.
Note 1: Apparently, just having the presence of 'container' in the $args list will cause the container to not be added. That is, whether the value assigned is 'false', false, 'foo' or 'bar', it doesn't matter; the container will be removed.
Note 2: 'container' => 'true' is the same as 'false', while 'container' => true (i.e., no quotes around true) will replace div in the container with 1. Something like <1 id="foo'...>.
<?php
function my_wp_nav_menu_args( $args = '' ) {
$args['container'] = false;
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
?>
OR
<?php wp_nav_menu( array( 'container' => '' ) ); ?>
This example will remove the ul around the list items.
<?php wp_nav_menu( array( 'items_wrap' => '%3$s' ) ); ?>
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>' ) ); ?>
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;
}
?>
If you are trying to customize the look of a specific menu item, e.g. Blog on single post pages, another simple option could be to use the body class "single".
For deeper conditional classes, you'll need to use a custom walker class (created in the 'walker' => new Your_Walker_Function argument).
The easiest way to build a new Walker class is to extend the core Walker_Nav_Menu class and override the bits you want to change.
This custom walker function will add several conditional classes to your nav menu (i.e. sub-menu, even/odd, etc):
class themeslug_walker_nav_menu extends Walker_Nav_Menu {
// add classes to ul sub-menus
function start_lvl( &$output, $depth = 0, $args = array() ) {
// depth dependent classes
$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent
$display_depth = ( $depth + 1); // because it counts the first submenu as 0
$classes = array(
'sub-menu',
( $display_depth % 2 ? 'menu-odd' : 'menu-even' ),
( $display_depth >=2 ? 'sub-sub-menu' : '' ),
'menu-depth-' . $display_depth
);
$class_names = implode( ' ', $classes );
// build html
$output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n";
}
// add main/sub classes to li's and links
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
global $wp_query;
$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent
// depth dependent classes
$depth_classes = array(
( $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ),
( $depth >=2 ? 'sub-sub-menu-item' : '' ),
( $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ),
'menu-item-depth-' . $depth
);
$depth_class_names = esc_attr( implode( ' ', $depth_classes ) );
// passed classes
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) );
// build html
$output .= $indent . '<li id="nav-menu-item-'. $item->ID . '" class="' . $depth_class_names . ' ' . $class_names . '">';
// link attributes
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$attributes .= ' class="menu-link ' . ( $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ) . '"';
$item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters( 'the_title', $item->title, $item->ID ),
$args->link_after,
$args->after
);
// build html
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
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' ) );
}
?>
Sometimes you may need to add a class to a menu item if it has submenus.
add_filter( 'wp_nav_menu_objects', 'add_menu_parent_class' );
function add_menu_parent_class( $items ) {
$parents = array();
foreach ( $items as $item ) {
if ( $item->menu_item_parent && $item->menu_item_parent > 0 ) {
$parents[] = $item->menu_item_parent;
}
}
foreach ( $items as $item ) {
if ( in_array( $item->ID, $parents ) ) {
$item->classes[] = 'menu-parent-item';
}
}
return $items;
}
Note: Since WordPress 3.7 classes .menu-item-has-children for wp_nav_menu , .page_item_has_children for wp_page_menu has been added to menus to indicate that an item has sub-items.
The following classes are applied to menu items, i.e. to the HTML <li> tags, generated by wp_nav_menu():
The following classes are added to maintain backward compatibility with the wp_page_menu() function output:
wp_nav_menu() is located in wp-includes/nav-menu-template.php.