improves menu item iteration

This commit is contained in:
buttle 2022-03-25 10:00:24 +01:00
parent 3f42b2f218
commit c48a6d5709
1 changed files with 2 additions and 17 deletions

View File

@ -27,28 +27,13 @@ foreach ($response_items as $menu_item) {
//print_r($items); //print_r($items);
if (!function_exists('menu_level_items')) {
function menu_level_items($items, $level_id) {
$level_items = new ArrayObject();
foreach ($items as $menu_level) {
foreach ($menu_level as $menu_item) {
if ($menu_item['menu_item_parent'] == $level_id) {
$level_items->append($menu_item);
}
}
}
return $level_items;
}
}
if (!function_exists('render_menu')) { if (!function_exists('render_menu')) {
function render_menu($items, $level_id) { function render_menu($items, $level_id) {
$level_items = menu_level_items($items, $level_id);
echo '<ul>' . PHP_EOL; echo '<ul>' . PHP_EOL;
foreach ($level_items as $menu_item) { foreach ($items[$level_id] as $menu_item) {
echo '<li>' . PHP_EOL; echo '<li>' . PHP_EOL;
echo '<a href="'.$menu_item['url'].'">'.$menu_item['title'].'</a>' . PHP_EOL; echo '<a href="'.$menu_item['url'].'">'.$menu_item['title'].'</a>' . PHP_EOL;
if (menu_level_items($items, $menu_item['ID'])) { if (array_key_exists($menu_item['ID'], $items)) {
render_menu($items, $menu_item['ID']); // render sub menu render_menu($items, $menu_item['ID']); // render sub menu
} }
echo '</li>' . PHP_EOL; echo '</li>' . PHP_EOL;