user1048676
user1048676

Reputation: 10066

Get Menu Values from Wordpress

All, I'm using the following code to get all of the defined wordpress menus that are created:

$menus = wp_get_nav_menus();

I know the ID of the menu I want to use. Based on the menu ID I'd like to get the Pages that are in that menu and the corresponding Navigation Label based on a selected menu ID. How can I go about doing that?

I actually discovered this:

$menu_items = wp_get_nav_menu_items($options['menu_choice']);

In that example the $options['menu_choice'] is the selected Menu ID but what I really would like is to give the permalink value. Can I get that from this?

Thanks for any help in advance!

Upvotes: 7

Views: 22139

Answers (5)

Pramod Jain
Pramod Jain

Reputation: 462

<pre> 
$menu_ID = '195'; // 195 is a menu id this id you can see 
http://example.com/wp-admin/nav-menus.php?action=edit&menu=195
$menu_args = array('menu' => $menu_ID );
wp_nav_menu( $menu_args );  
</pre>

Upvotes: 0

soipo
soipo

Reputation: 493

To get the post ID then you will have to pull it using this function :

$id = get_post_meta( $menu_item->ID, '_menu_item_object_id', true );

Otherwise the id will be the nav_menu custom type post that wordpress uses for the menus. Same goese for the $url, you can call it using get_permalink($id);

Upvotes: 1

kpotehin
kpotehin

Reputation: 1035

That's exactly what you want.

$menu_name = 'menu-id'; //e.g. primary-menu; $options['menu_choice'] in your case

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);
}

Now $menu_items is an object that contains all data for all menu items. So you can retrieve necessary data using foreach loop.

foreach ($menu_items as $menu_item) {
    $id = $menu_item->ID;
    $title = $menu_item->title;
    $url = $menu_item->url;

    if ($parent_id = $menu_item->menu_item_parent) {
        //the element has a parent with id $parent_id, so you can build a hierarchically menu tree
    }
    else {
        //the element doesn't have a parent
    }
}

You can find more interesting information for this question, such as orderby options, on official website: http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items

Upvotes: 9

Shebo
Shebo

Reputation: 297

You want to display a specific menu? why not use a simpler function, wp_nav_menu, and pass an argument of your desirable menu-ID? just replace your menu_id with $menu_ID in the next example:

<?php 
$menu_args = array('menu' => $menu_ID );
wp_nav_menu( $menu_args ); 
?> 

Upvotes: 2

Sam Margulies
Sam Margulies

Reputation: 156

To access the title and url of each item in a menu using the wp_get_nav_menu_items() function:

$menu_items = wp_get_nav_menu_items( $options['menu_choice'] );
foreach ( (array) $menu_items as $key => $menu_item ) {
    $title = $menu_item->title;
    $url = $menu_item->url;
}

Upvotes: 4

Related Questions