Challenge - 5 Problems
Navigation Walker Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does a custom walker class affect WordPress menu output?
You create a custom walker class extending
Walker_Nav_Menu and override the start_el method to add a data-id attribute to each menu item. What will be the visible effect on the rendered menu?Wordpress
class Custom_Walker extends Walker_Nav_Menu { function start_el(&$output, $item, $depth = 0, $args = null, $id = 0) { $output .= '<li data-id="' . $item->ID . '">' . $item->title . '</li>'; } } wp_nav_menu(array('walker' => new Custom_Walker()));
Attempts:
2 left
💡 Hint
Think about what the start_el method controls in the menu output.
✗ Incorrect
The start_el method controls how each menu item starts rendering. By adding a with a data-id attribute, each menu item will have that attribute in the HTML output.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this custom walker class
Which option contains a syntax error that will cause the custom walker class to fail?
Wordpress
class My_Walker extends Walker_Nav_Menu { function start_lvl(&$output, $depth = 0, $args = null) { $output .= "<ul class=\"sub-menu\">"; } function end_lvl(&$output, $depth = 0, $args = null) { $output .= "</ul>"; } function start_el(&$output, $item, $depth = 0, $args = null, $id = 0) { $output .= "<li>" . $item->title . "</li>"; } }
Attempts:
2 left
💡 Hint
Check how quotes are used inside strings.
✗ Incorrect
The string "
❓ state_output
advanced2:00remaining
What is the output of this walker modifying menu item classes?
Given this custom walker that adds a class 'highlight' to menu items with a title longer than 5 characters, what will be the class attribute of the menu item with title 'About Us'?
Wordpress
class Highlight_Walker extends Walker_Nav_Menu { function start_el(&$output, $item, $depth = 0, $args = null, $id = 0) { $classes = empty($item->classes) ? array() : (array) $item->classes; if(strlen($item->title) > 5) { $classes[] = 'highlight'; } $class_names = join(' ', $classes); $output .= '<li class="' . $class_names . '">' . $item->title . '</li>'; } }
Attempts:
2 left
💡 Hint
Count the characters in 'About Us' including spaces.
✗ Incorrect
'About Us' has 8 characters including the space, which is greater than 5, so the class 'highlight' is added to the classes array and output in the class attribute.
🔧 Debug
advanced2:00remaining
Why does this custom walker cause a fatal error?
This custom walker class causes a fatal error when used in wp_nav_menu. What is the cause?
Wordpress
class Broken_Walker extends Walker_Nav_Menu { function start_el($output, $item, $depth = 0, $args = null, $id = 0) { $output .= '<li>' . $item->title . '</li>'; } }
Attempts:
2 left
💡 Hint
Check how $output is passed and modified in walker methods.
✗ Incorrect
The $output parameter must be passed by reference (&$output) so that changes inside the method affect the original variable. Missing & causes fatal error because $output is treated as a copy and cannot be appended properly.
🧠 Conceptual
expert2:00remaining
What is the main purpose of a navigation walker class in WordPress?
Choose the best description of what a navigation walker class does in WordPress menus.
Attempts:
2 left
💡 Hint
Think about what part of the menu output the walker controls.
✗ Incorrect
A navigation walker class customizes the HTML output of menus by overriding methods that generate the start and end of menu items and levels. It does not handle database queries, permissions, or CSS generation.