Bird
Raised Fist0
Wordpressframework~10 mins

Navigation walker classes in Wordpress - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a custom walker class by extending the correct WordPress class.

Wordpress
class My_Custom_Walker extends [1] {
    // Custom walker code here
}
Drag options to blanks, or click blank then click option'
AWalker_Nav_Menu
BWalker_Page
CWalker_Category
DWalker_Comment
Attempts:
3 left
💡 Hint
Common Mistakes
Extending the wrong walker class like Walker_Page or Walker_Comment.
Not extending any class at all.
2fill in blank
medium

Complete the method signature to override the start of a menu item output.

Wordpress
public function [1](&\$output, \$item, \$depth = 0, \$args = null, \$id = 0) {
    // Custom code here
}
Drag options to blanks, or click blank then click option'
Aend_lvl
Bstart_el
Cstart_lvl
Dend_el
Attempts:
3 left
💡 Hint
Common Mistakes
Using end_el instead of start_el.
Confusing element methods with level methods.
3fill in blank
hard

Fix the error in the method name to properly close a submenu level.

Wordpress
public function [1](&\$output, \$depth = 0, \$args = null) {
    \$indent = str_repeat("\t", \$depth);
    \$output .= "\n" . \$indent . "</ul>\n";
}
Drag options to blanks, or click blank then click option'
Astart_el
Bstart_lvl
Cend_lvl
Dend_el
Attempts:
3 left
💡 Hint
Common Mistakes
Using start_lvl instead of end_lvl.
Using element methods instead of level methods.
4fill in blank
hard

Fill both blanks to add a CSS class to the menu item and output the link tag.

Wordpress
public function start_el(&\$output, \$item, \$depth = 0, \$args = null, \$id = 0) {
    \$classes = empty(\$item->classes) ? array() : (array) \$item->classes;
    \$classes[] = [1];
    \$class_names = join(' ', array_filter(\$classes));
    \$output .= '<li class="' . esc_attr(\$class_names) . '">';
    \$output .= '<a href="' . esc_url(\$item->url) . '">' . [2] . '</a>';
}
Drag options to blanks, or click blank then click option'
Acustom-menu-item
Besc_html(\$item->title)
Chtmlspecialchars(\$item->title)
Dstrip_tags(\$item->title)
Attempts:
3 left
💡 Hint
Common Mistakes
Not escaping the menu item title properly.
Forgetting to add a CSS class or adding invalid class names.
5fill in blank
hard

Fill both blanks to create a submenu wrapper with a custom class and indentation.

Wordpress
public function start_lvl(&\$output, \$depth = 0, \$args = null) {
    \$indent = str_repeat("\t", [1]);
    \$classes = array([2]);
    \$class_names = implode(' ', \$classes);
    \$output .= "\n" . \$indent . '<ul class="' . esc_attr(\$class_names) . '">\n';
}
Drag options to blanks, or click blank then click option'
A\$depth
B"sub-menu custom-submenu"
C"sub-menu"
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed number for indentation instead of depth.
Not including the required 'sub-menu' class.
Passing classes as a single string without array.

Practice

(1/5)
1. What is the main purpose of a custom navigation walker class in WordPress?
easy
A. To customize the HTML output of WordPress menus
B. To create new database tables for menu items
C. To change the WordPress admin dashboard layout
D. To add new user roles automatically

Solution

  1. Step 1: Understand the role of navigation walkers

    Navigation walker classes control how WordPress builds the HTML for menus.
  2. Step 2: Identify what customization means here

    Custom walkers let you change the HTML structure or classes of menu items.
  3. Final Answer:

    To customize the HTML output of WordPress menus -> Option A
  4. Quick Check:

    Navigation walker = customize menu HTML [OK]
Hint: Walkers change menu HTML output, not database or admin [OK]
Common Mistakes:
  • Thinking walkers create database tables
  • Confusing walkers with admin dashboard customization
  • Assuming walkers add user roles
2. Which method should you override in a custom walker class to change how each menu item starts rendering?
easy
A. start_el
B. end_el
C. start_lvl
D. end_lvl

Solution

  1. Step 1: Recall walker method roles

    In Walker_Nav_Menu, start_el controls the start of each menu item element.
  2. Step 2: Differentiate from other methods

    end_el ends a menu item, start_lvl and end_lvl handle submenu levels.
  3. Final Answer:

    start_el -> Option A
  4. Quick Check:

    start_el = start menu item HTML [OK]
Hint: start_el begins menu item output, others handle endings or levels [OK]
Common Mistakes:
  • Overriding end_el instead of start_el for item start
  • Confusing start_lvl with item rendering
  • Using end_lvl to start menu items
3. Given this custom walker snippet, what HTML tag will wrap each menu item?
class My_Walker extends Walker_Nav_Menu {
  function start_el(&$output, $item, $depth=0, $args=null, $id=0) {
    $output .= "
" . $item->title . "
"; } } wp_nav_menu(array('walker' => new My_Walker()));
medium
A. <li>
B. <div>
C. <span>
D. <a>

Solution

  1. Step 1: Analyze the start_el method output

    The method appends a string starting with <div> and ending with </div> wrapping the item title.
  2. Step 2: Understand the effect on menu HTML

    This replaces the default <li> tag with a <div> tag for each menu item.
  3. Final Answer:

    <div> -> Option B
  4. Quick Check:

    Custom start_el outputs <div> tags [OK]
Hint: Look inside start_el output string for tag used [OK]
Common Mistakes:
  • Assuming default <li> remains
  • Confusing wrapper tag with link tag
  • Ignoring custom output in start_el
4. What is wrong with this custom walker class code?
class Custom_Walker extends Walker_Nav_Menu {
  function start_el($output, $item, $depth=0, $args=null, $id=0) {
    $output .= "
  • " . $item->title . "
  • "; } }
    medium
    A. The class should extend Walker_Page, not Walker_Nav_Menu
    B. The method name should be end_el, not start_el
    C. The $item parameter is missing
    D. The $output parameter should be passed by reference

    Solution

    1. Step 1: Check method signature requirements

      In Walker_Nav_Menu, start_el must accept &$output by reference to modify it properly.
    2. Step 2: Identify the error in parameter passing

      The code uses $output without & reference, so changes won't affect the original output string.
    3. Final Answer:

      The $output parameter should be passed by reference -> Option D
    4. Quick Check:

      $output must be & passed to modify output [OK]
    Hint: Remember $output needs & to update menu HTML [OK]
    Common Mistakes:
    • Forgetting & on $output parameter
    • Mixing start_el with end_el method
    • Extending wrong walker class
    5. You want to add a custom CSS class to only top-level menu items using a custom walker. Which approach correctly achieves this?
    hard
    A. Override end_lvl and add class to submenu <ul> elements
    B. Override start_lvl and add class to all menu items regardless of depth
    C. Override start_el and check if $depth == 0, then add class to <li>
    D. Override end_el and add class to all <a> tags

    Solution

    1. Step 1: Understand $depth parameter meaning

      $depth indicates the menu level; 0 means top-level items.
    2. Step 2: Choose method to modify <li> classes

      start_el outputs the opening <li> tag, so adding class here for $depth == 0 targets top-level items only.
    3. Final Answer:

      Override start_el and check if $depth == 0, then add class to <li> -> Option C
    4. Quick Check:

      start_el + $depth check = top-level class [OK]
    Hint: Use $depth in start_el to target top-level items [OK]
    Common Mistakes:
    • Adding classes in start_lvl or end_lvl which affect submenus
    • Adding classes to all items without depth check
    • Modifying end_el which closes tags, not starts
    Recommended

    Ready to prove it?

    Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

    10 questions~5 mineasy hard