What does a walker menu mean?

时间:2019-10-04 作者:Star Light

我不明白什么是助行器菜单wp_nav_menu() 功能意味着。The document 没有说太多。它在图形或HTML中是什么样子的?

1 个回复
最合适的回答,由SO网友:Jess_Pinkman 整理而成

Walker\\u Nav\\u Menu是处理导航菜单的核心类。您可以使用它来处理导航菜单,也可以创建自己的子类来扩展该类。

如果决定使用自己的类,那么在使用wp\\u nav\\u menu()时必须将实例作为参数传递

class myWalkerClass Extends \\Walker_Nav_Menu {
 //overwrite any method as you see fit.
}

$args = [
  \'container\' => \'div\',
  //...other arguments,
  \'walker\' => new myWalkerClass(),
];

wp_nav_menu( $args );
如果你想让核心类处理你的导航菜单,就不要把它作为参数传递。

编辑:子PHP类的示例:

class myWalkerChildClass Extends \\Walker_Nav_Menu {

  /*there are 4 methods that can be overwritten ( meaning that if you declare them here, you are using your own method, and if you dont declare them here, you are using the original method from Walker_Nav_Menu parent PHP class.*/


    /*please note that fore very method, $output is passed by reference, meaning you can modify $output directly in the method, and changes carry over to the next method until it is printed.*/

    function start_lvl(&$output, $depth, $args ) {

        $output .= "\\n<ul>\\n"; //here you are starting a new list,
    }

    function end_lvl(&$output, $depth, $args ) {
        $output .= "</ul>\\n"; //here you are closing a list,
    }


    function start_el(&$output, $item, $depth, $args ) {
        $output. = "<li>".esc_attr($item->label); //here you are starting a new item within a list.
    }

    function end_el(&$output, $item, $depth, $args ) {
        $output .= "</li>\\n"; //and finally closing the item
    }
}

}
有关更深入的教程,请检查here:

相关推荐

如何在WordPress中覆盖/定制wp-admin/nav-menus.php

我想知道是否有办法定制wp管理/导航菜单。php,以便我可以添加自己的额外字段或选项卡。例如,我想为用户创建的每个菜单创建一个新选项卡(菜单设置)。我觉得如果在创建的每个菜单上都附加了特定的设置,会更加方便用户。这可能吗?目前,我正在按主题选项进行此操作,但它有局限性:(