您在中没有所需的数据start_lvl()
方法来执行此操作。该数据可在start_el()
中的方法$item
变量,因此您必须制定一种方法来传递该数据:
class my_extended_walker extends Walker_Nav_Menu {
var $prnt = 0;
var $pdepth = false;
public function start_lvl( &$output, $depth = 0, $args = array() ) {
$id = \'\';
if (!empty($this->prnt)) {
$id = \'id="prnt-\'.$this->prnt.\'"\';
}
$indent = str_repeat( "\\t", $depth );
$output .= "\\n$indent<ul $id class=\\"collapse\\">\\n";
}
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
if ($this->pdepth !== $depth) {
$this->prnt = $item->ID;
$this->pdepth = $depth;
} else {
$this->prnt = $this->pdepth = 0;
}
parent::start_el($output,$item,$depth,$args,$id);
}
}
// testing
wp_nav_menu(
array(
\'walker\'=>new my_extended_walker(),
\'menu\' => \'mymenu\'
)
);