0

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)?

1 Answer 1

0

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! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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