Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Menu Values from Wordpress

Tags:

php

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!

like image 578
user1048676 Avatar asked Mar 14 '12 19:03

user1048676


People also ask

How do I fetch menu items in WordPress?

Show activity on this post. Then do everything you want with this array like so: $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... } }

How do I get a list of menu names in WordPress?

Get it done in simple way! The code snippet is very simple as the following: $menus = get_terms( 'nav_menu' ); $menus = array_combine( wp_list_pluck( $menus, 'term_id' ), wp_list_pluck( $menus, 'name' ) ); print_r( $menus ); // You'll get: // array( 'menu_id' => 'Menu Name' );

How do I find menu item ID in WordPress?

Log into WordPress and go to Appearance > Menus and select the menu you want to get the ID of.

What is the Wp_nav_menu () function used for?

Usage. wp_nav_menu( $args ); Given a theme_location parameter, the function displays the menu assigned to that location. If no such location exists or no menu is assigned to it, the parameter fallback_cb will determine what is displayed.

How do I add a menu to the REST API?

Here are two functions that can be used to add the menu to the REST API and retrieve individual menu items: Create a shortcode that takes a menu ID and prints a simple list of links. Add this to your functions.php file. . $menu_html .

How to get the name of a menu object?

You can get the name like this, using the menu location, so if the menu is updated or you assign other menu you dont have to update anything here: $locations = get_nav_menu_locations(); //get all menu locations $menu = wp_get_nav_menu_object($locations['name_of_the_menu_location']);//get the menu object echo $menu->name; // name of the menu

How do I add a sub-menu to the header?

Show activity on this post. If your theme uses has_nav_menu ('<menu-name>') in the header or footer.php (for example), you can go to Appearance > Menus and select which menu or sub-menu to configure. Highly active question.

How do I get the metadata for MY MENU?

You can access the menu metadata using the wp_get_nav_menu_object function Object ( term_id => 4 name => My Menu Name slug => my-menu-name term_group => 0 term_taxonomy_id => 4 taxonomy => nav_menu description => parent => 0 count => 6 ) You are already supplying the name, why would you want to get it again?


2 Answers

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

like image 121
kpotehin Avatar answered Sep 23 '22 13:09

kpotehin


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;
}
like image 38
Sam Margulies Avatar answered Sep 22 '22 13:09

Sam Margulies