Codex tools: Log in
Languages: English • 日本語 • Português do Brasil • (Add your language)
Contents |
Displays a navigation menu created in the Appearance → Menus 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
<?php wp_nav_menu( $args ); ?>
<?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' => );
?>
Note: passes $args to the custom function.
<div class="access"> <?php wp_nav_menu(); ?> </div>
<?php wp_nav_menu( array('menu' => 'Project Nav' )); ?>
<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 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' => '' ) ); ?>
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;
}
?>
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.
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' ) );
}
?>
wp_nav_menu() is located in wp-includes/nav-menu-template.php.
Navigation Menu: register_nav_menus(), register_nav_menu(), unregister_nav_menu(), has_nav_menu(), wp_nav_menu(), wp_get_nav_menu_items()