21

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.

4 Answers 4

16

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

61

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

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

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;

}
1
  • Hey Pete how do you apply an active close to this?
    – RMH
    Mar 4, 2021 at 1:18
3

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;                       

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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