Pregunta

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?

¿Fue útil?

Solución

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>',
    ]
);
Licenciado bajo: CC-BY-SA con atribución
scroll top