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: