0
votes

for my Wordpress websites, I usually used wp_nav_menu() function to show a custom menu. But this time, I want to have all the menu elements in an array in php like that :

array(
    "Menu 1" => array("title" => "Menu 1", "url" => "http://...", "subitems"=> array(...),
    "Menu 2" => ...
)

It's just an exemple. I try to get the menu with SQL, but I didn't find anything that working very well. My menu contains pages, categories and external links. And to complete that, my site use WPML, so there is other menus...

Can you help me ?

1
Thanks, I think I can make what I want with that :)DarkCid

1 Answers

0
votes

I suggest you use the wp_get_nav_menu_items()-function to retrieve the menus, and from there select whatever few elements of them you want to keep.

For instance like so:

// the name of your menu
$menu_name = 'custom_menu_slug';

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 ) {
        // build the array
    }
}

More here: https://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items