解决方案:将以下代码复制粘贴到函数中。然后在模板使用中
my_nav_menu($menu_location);
//put this in your functions
class MY_Menu_Walker_Ext extends Walker {
var $tree_type = array(\'post_type\', \'taxonomy\', \'custom\');
var $db_fields = array(\'parent\' => \'menu_item_parent\', \'id\' => \'db_id\');
function start_el(&$output, $object, $depth = 0, $args = array(), $current_object_id = 0) {
$output .="<li><h2 class=\'dropdown-text\'>{$object->title}</h2>";
}
function end_el(&$output, $object, $depth = 0, $args = array()) {
$output.=\'</li>\';
}
}
class my_custom_menu {
public $menu;
public $menuItems;
public $parents;
public $walker;
public function __construct($menu_location) {
$this->setMenu($menu_location);
$this->getMenuItems();
$this->getParents();
$this->walker = new MY_Menu_Walker_Ext();
}
public function drawMenu() {
}
public function setMenu($menu_location) {
$this->menu = $this->getMenuByLocation($menu_location);
}
protected function getMenuByLocation($menu_location) {
$locations = get_nav_menu_locations();
$menu = null;
if ($locations && isset($locations[$menu_location])) {
$menu = wp_get_nav_menu_object($locations[$menu_location]);
}
return $menu;
}
public function get() {
}
public function getMenuItems() {
if ($this->menuItems)
return $this->menuItems;
$this->menuItems = wp_get_nav_menu_items($this->menu);
return $this->menuItems;
}
public function getParents() {
if ($this->parents)
return $this->parents;
$parents = array();
foreach ($this->menuItems as $item) {
if ($item->menu_item_parent == 0) {
array_push($parents, $item);
}
}
$this->parents = $parents;
return $this->parents;
}
public function getChild($parent_id) {
$childs = array();
foreach ($this->menuItems as $item) {
if ($parent_id == $item->menu_item_parent) {
$item->menu_item_parent = 0;
array_push($childs, $item);
foreach ($this->menuItems as $item1) {
if ($item->ID == $item1->menu_item_parent) {
array_push($childs, $item1);
}
}
}
}
return $childs;
}
public function draw() {
echo "<div class=\'postit-surround\'>";
foreach ($this->parents as $item) {
$this->displayParentHTML($item->title);
$this->drawChildren($this->getChild($item->ID));
}
echo "</div>";
}
public function displayParentHTML($title) {
?>
<a href="#">
<div class="postit">
<div class="pin">
<img src="<?php echo get_template_directory_uri(); ?>/assets/drawing-pin.png">
</div>
<div class="postit-title">
<h1 class="nav-title-text"><?php echo $title ?></h1>
</div>
<div class="corner-peel">
<img src="<?php echo get_template_directory_uri(); ?>/assets/corner-flick-cyan.png">
</div>
</div>
</a>
<?php
}
public function drawChildren($children) {
$defaults = array(\'menu\' => \'\', \'container\' => \'div\', \'container_class\' => \'\', \'container_id\' => \'\', \'menu_class\' => \'menu\', \'menu_id\' => \'\',
\'echo\' => true, \'fallback_cb\' => \'wp_page_menu\', \'before\' => \'\', \'after\' => \'\', \'link_before\' => \'\', \'link_after\' => \'\', \'items_wrap\' => \'<ul id="%1$s" class="%2$s">%3$s</ul>\',
\'depth\' => 0, \'walker\' => \'\', \'theme_location\' => \'\');
$args = array(
\'theme_location\' => \'header-menu\',
\'container\' => \'div\',
\'container_class\' => \'navigation-dropdown\',
\'items_wrap\' => \'<ul >%3$s</ul>\',
\'depth\' => 0,
);
$args = wp_parse_args($args, $defaults);
echo "<div class=\'navigation-dropdown\'><ul>";
echo $this->walker->walk($children, 2, $args);
echo "</ul></div>";
}
}
function my_nav_menu($name = null) {
$myMenu = new my_custom_menu($name);
$myMenu->draw();
}