13

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.

9

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

41

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.'" title="'.$navItem->title.'">'.$navItem->title.'</a></li>';

}
  • 1
    to me this returns "false" – John Smith Jan 24 '18 at 9:43
  • 4
    Are you copy-pasting @JohnSmith? You'll need to use your menu's ID. – Brad Adams Feb 22 '18 at 1:56
  • I found this far easier than using the walker class. Nice one! – Lee Mar 27 at 18:48
1

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;                       

?>
  • This is a decent answer, however it only goes 1 level deep. – Hybrid Web Dev Sep 11 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.