Retrieving a list of menu items in an array

I am currently am working on a custom theme. In this theme, I have a menu that I registered using register-nav-menu, and then I am displaying the menu to my web page using the wp_nav_menu function.

However, I like to retrieve the list of menu items only (the actual menu item names only that were used in wp-admin area to create the menu, and without any html), and I would like save that list of items to an array (i.e, not display it).

So, a psuedo code would look like this

$menu_items = get_list_of_items($name_of_menu); // $name_of_menu is the result of register-nav-menu, and get_list_of_items is the psuedo function
foreach ($menu_items as $item) {
    // do something with the item
}

Is there a way of doing so? Thanks.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

wp_get_nav_menu_items would appear to be exactly what you require. It returns an array of menu objects.

Method 2

As @vancoder mentions, wp_get_nav_menu_items() is the way to go, however I think a better detailed answer would be more helpful for people in the future.

Said function returns an array of WP_Post Object objects (so you access the values with an arrow, eg. $item->title).

For a basic setup, you could use the following:

$menuLocations = get_nav_menu_locations(); // Get our nav locations (set in our theme, usually functions.php)
                                           // This returns an array of menu locations ([LOCATION_NAME] = MENU_ID);

$menuID = $menuLocations['primary']; // Get the *primary* menu ID

$primaryNav = wp_get_nav_menu_items($menuID); // Get the array of wp objects, the nav items for our queried location.

Then you can just loop over that $primaryNav variable, for example:

foreach ( $primaryNav as $navItem ) {

    echo '<li><a href="'.$navItem->url.'" rel="nofollow noreferrer noopener" title="'.$navItem->title.'">'.$navItem->title.'</a></li>';

}

Method 3

Modified version of above with recursive loops.

Add To Functions.php

function wp_get_menu_array($current_menu='Main Menu') {

    $menu_array = wp_get_nav_menu_items($current_menu);

    $menu = array();

    function populate_children($menu_array, $menu_item)
    {
        $children = array();
        if (!empty($menu_array)){
            foreach ($menu_array as $k=>$m) {
                if ($m->menu_item_parent == $menu_item->ID) {
                    $children[$m->ID] = array();
                    $children[$m->ID]['ID'] = $m->ID;
                    $children[$m->ID]['title'] = $m->title;
                    $children[$m->ID]['url'] = $m->url;
                    unset($menu_array[$k]);
                    $children[$m->ID]['children'] = populate_children($menu_array, $m);
                }
            }
        };
        return $children;
    }

    foreach ($menu_array 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'] = populate_children($menu_array, $m);
        }
    }

    return $menu;

}

Method 4

Get simple array of menu.

Add To Functions.php

    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;
}

Go to Your Header And Call Function

<?php
 $a = wp_get_menu_array('top-menu-header');                         
 print_r($a); exit;                       

?>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x