WordPress wp_nav_menu Function [Full Guide]

WordPress wp_nav_menu function

What is the wp_nav_menu() function used for?

The wp_nav_menu() function is an essential tool in WordPress for creating navigation menus that can be customized within a theme. It acts as a template tag, making it easy to integrate dynamic menus into a WordPress site. Through the WordPress admin interface, users can easily manage and customize their navigation structure.

Developers can utilize this function in theme files to display menus in different areas, such as the header or footer, by specifying the menu location or ID. By using its parameters, developers have control over the menu’s appearance, including options like adding CSS classes, modifying the container and item elements, and incorporating fallback choices for menus that are not assigned.

Ultimately, wp_nav_menu() simplifies the process of incorporating user-friendly and flexible navigation menus into WordPress themes, thereby enhancing the overall user experience.

Description of wp_nav_menu() Parameters

Basic syntax:


wp_nav_menu(array $args = array());


The wp_nav_menu() function in WordPress provides developers with the flexibility to modify the behavior and appearance of the navigation menu. Within the $args array, there are several commonly used parameters that can be utilized for customization.

  1. menu: (int|string|WP_Term) Specifies which menu to display by either its ID, name, or WP_Term object.
  2. container: (string|bool) Defines the HTML container element for the menu. Default is ‘div’, set to false for no container.
  3. container_id: (string) Specifies the ID attribute of the container element.
  4. container_class: (string) Defines the class attribute of the container element.
  5. menu_class: (string) Specifies the class attribute for the <ul> element containing the menu items.
  6. menu_id: (string) Sets the ID attribute of the <ul> element containing the menu items.
  7. echo: (bool) Determines whether to echo the menu or return it as a string. Default is true (echo).
  8. fallback_cb: (callable|string) Defines a fallback function to be called if the menu doesn’t exist. Default is ‘wp_page_menu’.
  9. before: (string) Adds content before each link within the menu.
  10. after: (string) Adds content after each link within the menu.
  11. link_before: (string) Adds content before the anchor text of each link.
  12. link_after: (string) Adds content after the anchor text of each link.
  13. items_wrap: (string) Specifies how the list items should be wrapped. Default is ‘<ul id=”%1$s” class=”%2$s”>%3$s</ul>’.
  14. depth: (int) Limits the depth of the menu. 0 means no limit.
  15. walker: (object) Custom walker class to use for rendering the menu. This allows for fine-grained control over the menu’s HTML output.
  16. there_location: (string) Theme location to be used. Must be registered with register_nav_menu() in order to be selectable by the user.
  17. items_wrap: (string) How the list items should be wrapped. Uses printf() format with numbered placeholders. The default is an ul with an id and class.
  18. item_spacing: (string) Whether to preserve whitespace within the menu’s HTML. Accepts ‘preserve’ or ‘discard’. Default ‘preserve’.

How to use wp_nav_menu() in WordPress

To use the wp_nav_menu() function in WordPress, you typically include it in your theme files, such as in the header.php or footer.php file, where you want the navigation menu to appear. Here’s a basic example:


// Display the default navigation menu

This basic usage will display the default navigation menu assigned in the WordPress admin dashboard under “Appearance” > “Menus.”

If you want to customize the menu and its display, you can use the $args parameter. Here’s an example with some common customization options:


// Define custom arguments
$menu_args = array(
    'theme_location' => 'primary-menu', // Menu location defined in your theme
    'container'      => 'nav',          // Use <nav> as the container
    'container_id'   => 'main-nav',     // ID attribute of the container
    'container_class'=> 'menu-container',// Class attribute of the container
    'menu_class'     => 'menu',         // Class attribute of the menu
    'echo'           => true,           // Echo the menu (true) or return it as a string (false)
    'fallback_cb'    => 'wp_page_menu', // Fallback function if the menu doesn't exist

// Display the custom navigation menu

In this instance, the theme_location parameter designates the menu location that you have defined in your theme. Additionally, the container, container_id, container_class, menu_class, and fallback_cb parameters allow for customization of the container and the appearance of the menu. You are welcome to modify these parameters according to your specific needs and preferences.

Ensure that you tailor the theme_location to correspond with the menu location you have established in your theme, and make any necessary adjustments to the other parameters to suit your design and functionality preferences.


The wp_nav_menu() function is a basic function that can be used to display and customize a menu in your WordPress theme. Feel free to use it to display custom menus in your WordPress theme.

About The Author

Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

The Ultimate Managed Hosting Platform

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.