wp_nav_menu()
wp_nav_menu()
is a core WordPress function used to display navigation menus in theme templates. It retrieves menus created in the WordPress Admin under Appearance → Menus and outputs them as HTML lists (<ul><li>
). Developers can control which menu is displayed, its container, CSS classes, depth, and even attach custom walkers for advanced customization.
How it works
- Called in theme files (often
header.php
) to display a registered menu. - Accepts an array of arguments such as:
theme_location
→ registered menu location.menu_class
→ custom CSS class for the<ul>
.depth
→ how many levels of hierarchy to display.walker
→ custom menu walker class.
- Integrates with the WordPress Menu system, so editors can manage links in the backend.
Why it matters
wp_nav_menu()
bridges the admin-defined menu structure and the frontend template. It gives developers a flexible way to control navigation markup while allowing content editors to manage menus without touching code.
Examples
wp_nav_menu([
'theme_location' => 'primary',
'menu_class' => 'nav navbar-nav',
'container' => false,
'depth' => 2,
]);