Displays a navigation menu created in the AppearanceMenus 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

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

Note: As of 3.5, if there are no menu items, no HTML markup will be output.

wp_nav_menu() 描述


 <?php wp_nav_menu$args ); ?> 

Usage (Showing Default Values)

wp_nav_menu() 用法



(string) (可选) The location in the theme to be used--must be registered with register_nav_menu() in order to be selectable by the user

默认值: None


(string) (可选) The menu that is desired; accepts (matching in order) id, slug, name

默认值: None


(string) (可选) 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

默认值: div


(string) (可选) The class that is applied to the container

默认值: menu-{menu slug}-container


(string) (可选) The ID that is applied to the container

默认值: None


(string) (可选) The class that is applied to the ul element which encloses the menu items. Multiple classes can be separated with spaces. Formerly known as $wrap_class.

默认值: menu


(string) (可选) The ID that is applied to the ul element which encloses the menu items. Formerly known as $wrap_id.

默认值: menu-{menu slug}; when there are duplicates, the next values are menu-{menu slug}-1, menu-{menu slug}-2, etc.


(boolean) (可选) Whether to echo the menu or return it. For returning menu use '0'

默认值: true


(string) (可选) If the menu doesn't exist, the fallback function to use. Set to false for no fallback. Note: Passes $args to the custom function.

默认值: wp_page_menu


(string) (可选) Output text before the <a> of the link

默认值: None


(string) (可选) Output text after the </a> of the link

默认值: None


(string) (可选) Output text before the link text

默认值: None


(string) (可选) Output text after the link text

默认值: None


(string) (可选) Evaluated as the format string argument of a sprintf() expression. The format string incorporates the other parameters by numbered token. %1$s is expanded to the value of the 'menu_id' parameter, %2$s is expanded to the value of the 'menu_class' parameter, and %3$s is expanded to the value of the list items. If a numbered token is omitted from the format string, the related parameter is omitted from the menu markup. Note: To exclude the items wrap (for instance, if the wrap is built into your theme), you still need to pass %3$s as the parameter. If you pass an empty string, your menu won't display at all.

默认值: <ul id="%1$s" class="%2$s">%3$s</ul>


(integer) (可选) How many levels of the hierarchy are to be included where 0 means all. -1 displays links at any depth and arranges them in a single, flat list.

默认值: 0


(object) (可选) Custom walker object to use (Note: You must pass an actual object to use, not a string)

默认值: new Walker_Nav_Menu

wp_nav_menu() 参数


Default example

Shows the first non-empty menu or wp_page_menu().

Targeting a specific Menu

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:

Used in the Twenty Ten theme

Removing the Navigation Container

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'...>.


Removing the ul wrap

This example will remove the ul around the list items.

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.

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.

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".

Using a custom Walker class

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):

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.

How to add a parent class for menu item

Sometimes you may need to add a class to a menu item if it has submenus.

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.

wp_nav_menu() 示例


  • 3.5.0: If a menu has no items, don't output any markup.
  • 添加于 版本 3.0.0

wp_nav_menu() 历史