How do I convert a custom menu wp_get_nav_menu_items into Fourth level?
how can I turn this function from Two-level into the Fourth level in WordPress? This function can only echo the parent and its child, but I'm trying to make it unlimited or at least 4 levels if it is possible
A
B -2
C -2
-3 -- Not Work
-4 -- Not Work
D -2
F
this is my code: it works only till level 2
function wp_get_menu_array($current_menu) {
$array_menu = wp_get_nav_menu_items($current_menu);
$menu = array();
foreach ($array_menu as $m) {
if (empty($m-menu_item_parent)) {
$menu[$m-ID] = array();
$menu[$m-ID]['ID'] = $m-ID;
$menu[$m-ID]['title'] = $m-title;
$menu[$m-ID]['url'] = $m-url;
$menu[$m-ID]['children'] = array();
}
}
$submenu = array();
foreach ($array_menu as $m) {
if ($m-menu_item_parent) {
$submenu[$m-ID] = array();
$submenu[$m-ID]['ID'] = $m-ID;
$submenu[$m-ID]['title'] = $m-title;
$submenu[$m-ID]['url'] = $m-url;
$menu[$m-menu_item_parent]['children'][$m-ID] = $submenu[$m-ID];
}
}
return $menu;
}
$menu_items = wp_get_menu_array('main-menu'); // Then call in your theme
nav
ul
?php foreach ($menu_items as $item) : ?
li
a href=?= $item['url'] ? title=?= $item['title'] ??= $item['title'] ?/a
?php if( !empty($item['children']) ):?
ul class=sub-menu
?php foreach($item['children'] as $child): ?
li class=b-main-header__sub-menu__nav-item
a href=?= $child['url'] ? title=?= $child['title'] ??= $child['title'] ?/a
/li
?php endforeach; ?
/ul
?php endif; ?
/li
?php endforeach; ?
ul
/nav