Menus help visitors find pages on your website easily. Navigation organizes links so users can move around smoothly.
Menus and navigation in Wordpress
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Wordpress
<?php wp_nav_menu(array( 'theme_location' => 'primary', 'menu_class' => 'main-menu', 'container' => 'nav', 'container_class' => 'main-navigation' )); ?>
theme_location links the menu to a registered spot in your theme.
menu_class adds CSS classes to style the menu.
Examples
Wordpress
<?php wp_nav_menu(array('theme_location' => 'footer')); ?>
Wordpress
<?php wp_nav_menu(array( 'theme_location' => 'primary', 'menu_class' => 'nav-list', 'container' => 'div', 'container_class' => 'nav-wrapper' )); ?>
Wordpress
<?php wp_nav_menu(array( 'theme_location' => 'sidebar', 'depth' => 2 )); ?>
Sample Program
This code registers two menu spots: primary and footer. Then it shows the primary menu inside a <nav> element with CSS classes for styling.
Wordpress
<?php // Register menu locations in functions.php function mytheme_register_menus() { register_nav_menus(array( 'primary' => __('Primary Menu'), 'footer' => __('Footer Menu') )); } add_action('after_setup_theme', 'mytheme_register_menus'); // Display the primary menu in header.php wp_nav_menu(array( 'theme_location' => 'primary', 'menu_class' => 'primary-menu', 'container' => 'nav', 'container_class' => 'primary-navigation' )); ?>
Important Notes
Always register menu locations in your theme before using them.
Menus can be managed in WordPress admin under Appearance > Menus.
Use CSS to style menus and make them responsive for mobile devices.
Summary
Menus organize links so visitors can navigate your site easily.
Use wp_nav_menu() to display menus in your theme.
Register menu locations first, then assign menus in WordPress admin.
Practice
1. What is the main purpose of using menus in a WordPress site?
easy
Solution
Step 1: Understand the role of menus
Menus group links to help users find pages and sections on the site.Step 2: Compare options to menu purpose
Only organizing links for easy navigation matches the menu's purpose.Final Answer:
To organize links so visitors can navigate the site easily -> Option CQuick Check:
Menus = Organize links for navigation [OK]
Hint: Menus help visitors find pages easily [OK]
Common Mistakes:
- Thinking menus add images or change colors
- Confusing menus with blog post creation
2. Which of the following is the correct way to display a registered menu in a WordPress theme?
easy
Solution
Step 1: Identify function to display menu
wp_nav_menu()is used to show menus in themes.Step 2: Check correct syntax for displaying menu
Passingarray('theme_location' => 'primary')tells WordPress which menu to show.Final Answer:
wp_nav_menu(array('theme_location' => 'primary')); -> Option BQuick Check:
Display menu = wp_nav_menu() with theme_location [OK]
Hint: Use wp_nav_menu() with theme_location to show menus [OK]
Common Mistakes:
- Using register_nav_menu() to display menus instead of register
- Trying to use non-existent functions like add_menu_location or show_menu
3. Given this code in a WordPress theme:
What will happen if no menu is assigned to the 'footer' location?
wp_nav_menu(array('theme_location' => 'footer'));What will happen if no menu is assigned to the 'footer' location?
medium
Solution
Step 1: Understand wp_nav_menu behavior without assigned menu
If no menu is assigned to the location, WordPress shows nothing by default.Step 2: Check if errors or fallback occur
By default, no error or crash happens; the menu area is empty.Final Answer:
Nothing will display, no menu shown -> Option AQuick Check:
No assigned menu = no output, no error [OK]
Hint: No menu assigned means no menu output [OK]
Common Mistakes:
- Expecting default page list to show automatically
- Thinking WordPress throws errors or crashes
4. You added this code to your theme's functions.php:
But the menu does not appear in the WordPress admin under Appearance > Menus > Manage Locations. What is the likely problem?
register_nav_menu('header-menu', 'Header Menu');But the menu does not appear in the WordPress admin under Appearance > Menus > Manage Locations. What is the likely problem?
medium
Solution
Step 1: Understand when to register menus
Menu registration code should run during theme setup, typically hooked toafter_setup_theme.Step 2: Check if code runs too early
Placingregister_nav_menu()directly in functions.php without hook may run too early, so WordPress doesn't register it properly.Final Answer:
The code must be inside a function hooked to after_setup_theme -> Option AQuick Check:
Register menus inside after_setup_theme hook [OK]
Hint: Hook menu registration to after_setup_theme [OK]
Common Mistakes:
- Using register_nav_menus() instead of register_nav_menu() is not required for one menu
- Flushing permalinks does not affect menu registration
- Assuming menus appear without registering locations
5. You want to create two menu locations in your theme: one for the header and one for the footer. Which code correctly registers both menus and displays them in the theme?
hard
Solution
Step 1: Register multiple menus correctly
Useregister_nav_menus()with an array of locations inside a function hooked toafter_setup_theme.Step 2: Display menus with correct syntax
Usewp_nav_menu()withtheme_locationkeys matching registered locations.Step 3: Check each option for correctness
function register_my_menus() { register_nav_menus(array( 'header' => 'Header Menu', 'footer' => 'Footer Menu' )); } add_action('after_setup_theme', 'register_my_menus'); // In header.php wp_nav_menu(array('theme_location' => 'header')); // In footer.php wp_nav_menu(array('theme_location' => 'footer'));correctly hooks registration toafter_setup_theme, usesregister_nav_menus(), and displays menus properly.Final Answer:
Option D code correctly registers and displays both menus -> Option DQuick Check:
Multiple menus = register_nav_menus + after_setup_theme + wp_nav_menu(theme_location) [OK]
Hint: Use register_nav_menus with after_setup_theme hook [OK]
Common Mistakes:
- Not hooking registration to after_setup_theme
- Using wp_nav_menu() without theme_location array
- Hooking registration to wrong action like init
