1

I'm creating a custom Walker to my menu, my code is like this:

<?php

class My_Walker extends Walker {
    function start_lvl( &$output, $depth = 0, $args = array() ) {
        $output .= "<nav>";
    }

    function end_lvl( &$output, $depth = 0, $args = array() ) {
        $output .= "</nav>";
    }

    function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {
        $output .= "<li>My Item</li>";  //This is just a sample <li>
    }
}

The start_el is working fine, but start_lvl and end_lvl is not working at all.

The rendered HTML is like this:

list code sample

Why is my start_lvl and end_lvl not working?

3

Your menu doesn't appear to have any levels. start_lvl and end_lvl are used for the sub-menu wrappers. The outer wrapper for the menu, the <ul> is defined by the items_wrap argument of wp_nav_menu():

wp_nav_menu(
    [
        'walker'     => new My_Walker(),
        'items_wrap' => '<nav id="%1$s" class="%2$s">%3$s</nav>',
    ]
);
  • Thanks! it works! – Lai32290 Sep 2 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.