How to get the wordpress menu array
BY SlUG
<?php $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>"; ?>
BY LOCATION
$menu_name = 'primary'; //Location $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' ) );
Output
Array ( [0] => WP_Post Object ( [ID] => 22 [post_author] => 1 [post_date] => 2016-06-21 12:58:35 [post_date_gmt] => 2016-06-21 12:58:35 [post_content] => [post_title] => [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => 22 [to_ping] => [pinged] => [post_modified] => 2016-06-22 05:18:52 [post_modified_gmt] => 2016-06-22 05:18:52 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/wordpress/?p=22 [menu_order] => 1 [post_type] => nav_menu_item [post_mime_type] => [comment_count] => 0 [filter] => raw [db_id] => 22 [menu_item_parent] => 0 [object_id] => 2 [object] => page [type] => post_type [type_label] => Page [url] => http://localhost/wordpress/sample-page/ [title] => Sample Page [target] => [attr_title] => [description] => [classes] => Array ( [0] => ) [xfn] => ) [1] => WP_Post Object ( [ID] => 29 [post_author] => 1 [post_date] => 2016-06-22 05:18:52 [post_date_gmt] => 2016-06-22 05:18:52 [post_content] => [post_title] => [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => 29 [to_ping] => [pinged] => [post_modified] => 2016-06-22 05:18:52 [post_modified_gmt] => 2016-06-22 05:18:52 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/wordpress/?p=29 [menu_order] => 2 [post_type] => nav_menu_item [post_mime_type] => [comment_count] => 0 [filter] => raw [db_id] => 29 [menu_item_parent] => 0 [object_id] => 24 [object] => page [type] => post_type [type_label] => Page [url] => http://localhost/wordpress/page-abc/ [title] => page abc [target] => [attr_title] => [description] => [classes] => Array ( [0] => ) [xfn] => ) [2] => WP_Post Object ( [ID] => 28 [post_author] => 1 [post_date] => 2016-06-22 05:18:52 [post_date_gmt] => 2016-06-22 05:18:52 [post_content] => [post_title] => [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => 28 [to_ping] => [pinged] => [post_modified] => 2016-06-22 05:18:52 [post_modified_gmt] => 2016-06-22 05:18:52 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/wordpress/?p=28 [menu_order] => 3 [post_type] => nav_menu_item [post_mime_type] => [comment_count] => 0 [filter] => raw [db_id] => 28 [menu_item_parent] => 29 [object_id] => 26 [object] => page [type] => post_type [type_label] => Page [url] => http://localhost/wordpress/page-def/ [title] => page def [target] => [attr_title] => [description] => [classes] => Array ( [0] => ) [xfn] => ) [3] => WP_Post Object ( [ID] => 30 [post_author] => 1 [post_date] => 2016-06-22 05:18:53 [post_date_gmt] => 2016-06-22 05:18:53 [post_content] => [post_title] => [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => 30 [to_ping] => [pinged] => [post_modified] => 2016-06-22 05:18:53 [post_modified_gmt] => 2016-06-22 05:18:53 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/wordpress/?p=30 [menu_order] => 4 [post_type] => nav_menu_item [post_mime_type] => [comment_count] => 0 [filter] => raw [db_id] => 30 [menu_item_parent] => 0 [object_id] => 8 [object] => page [type] => post_type [type_label] => Page [url] => http://localhost/wordpress/ [title] => xcvxxv [target] => [attr_title] => [description] => [classes] => Array ( [0] => ) [xfn] => ) )
- If the menu_item_parent equals 0 (false) that manse that menu is a root menu
- menu_item_parent hold the parent menu id if it is not 0
ref: https://codex.wordpress.org/Function_Reference/wp_get_nav_menu_object
https://developer.wordpress.org/reference/functions/wp_get_nav_menu_items/