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.

4

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

Leave a Comment