How to get menu items in Wordpress?
I want to get my main wp menu items. I tried the code but they all only return a blank page. I donโt know which method I should use and I donโt know if I need to include any other page in my page?
And is there a way to get menu items from the database directly without using wp methods and functions? So far, I could not understand the structure of the wp table. So I don't know exactly the relationship between the tables.
Thank.
source to share
If you know your menu location id (usually declared in the .php function on "register_nav_menus"), you can use this snippet:
// GET ALL MENU OBJECTS AT SPECIFIED LOCATION
function yourprefix_get_menu_items($location_id){
//$locations = get_registered_nav_menus();
$menus = wp_get_nav_menus();
$menu_locations = get_nav_menu_locations();
if (isset($menu_locations[ $location_id ]) && $menu_locations[ $location_id ]!=0) {
foreach ($menus as $menu) {
if ($menu->term_id == $menu_locations[ $location_id ]) {
$menu_items = wp_get_nav_menu_items($menu);
break;
}
}
return $menu_items;
}
}
Or a shorter version from codex:
function yourprefix_get_menu_items($menu_name){
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
return wp_get_nav_menu_items($menu->term_id);
}
}
Then do whatever you want with an array like this:
$menu_items = yourprefix_get_menu_items('sidebar-menu'); // replace sidebar-menu by desired location
if(isset($menu_items)){
foreach ( (array) $menu_items as $key => $menu_item ) {
...some code...
}
}
And here is a link to all the nav_menu data that you can select directly from the database on the mysql query: http://lasota.community.uaf.edu/2011/07/29/nav-menu-data-location-in-wordpress- 3-2 /
source to share
<?php
$menu = 'menu-name/menu-id';
$args = array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'nav_menu_item',
'post_status' => 'publish',
'output' => ARRAY_A,
'output_key' => 'menu_order',
'nopaging' => true,
'update_post_term_cache' => false );
$items = wp_get_nav_menu_items( $menu, $args );
?>
source to share
You can try this
$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>";
You will get your Object menu
source to share
You can do it:
add_action('wp_nav_menu_menuname_items', 'user_nav_menu_items');
function user_nav_menu_items( $items ) {
/* do something with $items (string) */
return $items;
}
If you want it to be an array, just blow it up and blow it up after you're done:
add_action('wp_nav_menu_menuname_items', 'user_nav_menu_items');
function user_nav_menu_items( $items ) {
$items_r = explode("<li", $items);
/* do something with $items_r (array) */
$items_new = implode("<li", $items_r);
return $items_new;
}
In both cases, you must replace menuname
the hook with add_action for the name of the menu where you are trying to get the items.
source to share
Use get_registered_nav_menus () function
<?php
$menus = get_registered_nav_menus();
foreach ( $menus as $location => $description ) {
echo $location . ': ' . $description . '<br />';
}
source to share