Consider this code snippet in a WordPress theme's functions.php file:
function register_my_menu() {
register_nav_menu('header-menu', __('Header Menu'));
}
add_action('init', 'register_my_menu');What does this code do when the theme is active?
function register_my_menu() {
register_nav_menu('header-menu', __('Header Menu'));
}
add_action('init', 'register_my_menu');Think about what register_nav_menu does versus creating menu items.
This code registers a single menu location named 'Header Menu' so users can assign menus to it in the admin panel. It does not create menu items or remove existing menus.
You want to display a menu assigned to the 'header-menu' location in your theme template. Which code snippet will correctly output the menu?
Look for the correct function and parameter format to display menus.
The wp_nav_menu function requires an array with the key theme_location to specify which menu location to display.
Given this code in a theme template:
<?php wp_nav_menu(array('theme_location' => 'footer-menu')); ?>And the admin menu settings show no menu assigned to 'footer-menu'. What is the reason no menu items appear?
<?php wp_nav_menu(array('theme_location' => 'footer-menu')); ?>
Check the admin menu assignment for the location.
If no menu is assigned to a registered menu location, wp_nav_menu outputs nothing by default.
Consider this custom walker class snippet:
class My_Walker extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth=0, $args=null, $id=0) {
$output .= '<li>' . strtoupper($item->title) . '</li>';
}
}
wp_nav_menu(array('theme_location' => 'header-menu', 'walker' => new My_Walker()));What will the menu items look like when rendered?
class My_Walker extends Walker_Nav_Menu { function start_el(&$output, $item, $depth=0, $args=null, $id=0) { $output .= '<li>' . strtoupper($item->title) . '</li>'; } } wp_nav_menu(array('theme_location' => 'header-menu', 'walker' => new My_Walker()));
Look at how the walker modifies the output string.
The custom walker overrides start_el to output each menu item title in uppercase inside <li> tags.
Choose the statement that correctly explains the relationship between menu locations and menus in WordPress.
Think about who creates menu locations and who creates menus.
The theme defines menu locations as placeholders. Users create menus and assign them to these locations to control navigation.