最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

menus - start_lvl on Walker is not working

programmeradmin0浏览0评论

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:

Why is my start_lvl and end_lvl not working?

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:

Why is my start_lvl and end_lvl not working?

Share Improve this question asked Aug 31, 2019 at 23:53 Lai32290Lai32290 3511 gold badge4 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 6

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>',
    ]
);
发布评论

评论列表(0)

  1. 暂无评论