外贸营销
获取 WordPress 菜单的树形结构数组(对象)
虽然 WordPress 提供了 wp_nav_menu 方法来输出菜单,但是如果你想实现超级菜单(Mega Menu),单靠它就做不到了。
好在有 wp_get_nav_menu_object 和 wp_get_nav_menu_items 这两个菜单接口,可以先取得扁平化的菜单数组,然后稍稍加工一下就能得到树形结构的菜单数组了。
function c7sky_get_nav_menu_tree( $location ) {$locations = get_nav_menu_locations();$menu_id = $locations[$location] ;$menu_object = wp_get_nav_menu_object($menu_id);$menu_items = wp_get_nav_menu_items($menu_object->term_id);_wp_menu_item_classes_by_context($menu_items);$menu = array();$submenus = array();foreach ($menu_items as $m) {$m->children = array();if (!$m->menu_item_parent) {$menu[$m->ID] = $m;} else {$submenus[$m->ID] = $m;if (isset($menu[$m->menu_item_parent])) {$menu[$m->menu_item_parent]->children[$m->ID] = &$submenus[$m->ID];} else {$submenus[$m->menu_item_parent]->children[$m->ID] = $submenus[$m->ID];}}}return $menu;}
标签