1

How do I get the menu items (depth = 1) into an array?

wp_nav_menu outputs a formatted list with both ul and li elements. wp_list_pages also outputs a formatted list with both ul and li.

I just want to get the menu items (striped of tags) of depth one into an array.

How do I accomplish this?

2 Answers 2

3

I think this will helps you: wp get nav menu items

    $menu_name = 'custom_menu_slug'; // Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_menu)

    if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
    $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );

    $menu_items = wp_get_nav_menu_items($menu->term_id);


    foreach ( (array) $menu_items as $key => $menu_item ) {
        $title = $menu_item->title;
    }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for answer. $menu_name will be the same as what the menu is called under the option "Appearance > Menus: Menu Name (for the menu I want to use)?
Works for me, without the location check.
Works like a charm. :)
0

Thanks @Libin

Figured out wp_get_nav_menu_items()

$menu_name = 'sidebar-menu'; //menu slug
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) );

echo "<pre>";
print_r($menuitems);
echo "</pre>";

here i am getting the whole menu items

I fount one example here http://wiki.workassis.com/wordpress-get-menu-array/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.