这可以做到:
class My_Walker_Nav_Menu extends Walker_Nav_Menu {
private $_before = null;
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
// Backup the original "before", if any.
if ( null === $this->_before ) {
$this->_before = $args->before;
}
// Now add the wrapper div, if applicable.
if ( 0 == $depth ) {
$args->before = \'<div class="sub-menu-wrapper">\' . $this->_before;
} else {
$args->before = $this->_before;
}
// Then let the parent walker does the output generation.
parent::start_el( $output, $item, $depth, $args, $id );
}
public function end_el( &$output, $item, $depth = 0, $args = array() ) {
// Close the wrapper div, if applicable.
if ( 0 == $depth ) {
$output .= \'</div><!-- .sub-menu-wrapper -->\';
}
$output .= "</li>\\n";
}
}
一、 e.扩展
start_el()
和
end_el()
方法/函数,而不是
start_lvl()
和
end_lvl()
. <此外,仅当菜单项有子菜单(或子菜单)时,才会添加包装罢工>
示例用法:
wp_nav_menu( [
\'walker\' => new My_Walker_Nav_Menu(),
// ... other args.
] );