Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Let's assume the client builds a custom menu with 3 menu items and I'd like to show a few posts, then the first menu item, then some more posts, the second menu item, more posts and then the third menu item.

Is there any special function to retrieve only the link of the first menu item?

Example menu:

- HOME
- ABOUT
- CONTACT

How can I get the link of the "HOME" menu item from a custom menu (Appearance -> Menu)?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Ok, real stupid while writing this post I was thinking about first looping of the menu items and then storing them in an array for later use. With the example on the following page I can manage that:

http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items

// Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_menu) // This code based on wp_nav_menu's code to get Menu ID from menu slug

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

$menu_list = '<ul id="menu-' . $menu_name . '">';

foreach ( (array) $menu_items as $key => $menu_item ) {
    $title = $menu_item->title;
    $url = $menu_item->url;
    $menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
}
$menu_list .= '</ul>';
} else {
$menu_list = '<ul><li>Menu "' . $menu_name . '" not defined.</li></ul>';
}
// $menu_list now ready to output
echo $menu_list

The code above just needs some adapting but it will do! :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.