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.

Original question How do I get menu items using slug and not ID

I need to get a specific menu, but I can't use ID, since the menu ID on my dev is not the same as on my staging and prod server.

WP doc says that wp_get_nav_menu_itemscan be used with menu slug. But how do I know what slug is created? I tested with what I thought would be a slug, but I got no menu items.

My initial though was that I needed to fetch a menu in order to get menu items in an array / object so that I could add an extra menu item.

There is little documentation on how one can get a menu item by using a slug, so I got a bit stuck. If you should have the answer, please add this as well.

My goal was to add a 'home' link based on a specific condition.

Thanks to the feedback, I have rewritten my question so it makes more sense ;)

share|improve this question
    
Four answers and no up vote. Guys, why did you think it was worth the time? –  toscho Mar 16 '12 at 19:24
    
I've not upvoted yet, because (as I noted in a comment, below) I think the question is not written well enough (it doesn't actually ask what the OP is trying to accomplish). Once the OP rephrases the question, I will gladly up-vote. –  Chip Bennett Mar 16 '12 at 20:24
    
Thanks for the edit; upvoted! –  Chip Bennett Mar 16 '12 at 21:17
add comment

4 Answers

up vote 2 down vote accepted

So, based on your comment:

wp_nav_menu is what I'm using now. But now I have to add 'home' menu item depending on certain criteria

I'm going to propose a solution that will allow you to add a "Home" menu item to any menu, based on arbitrary conditions, using the wp_nav_menu_items filter (see tutorial here):

<?php
function wpse45802_add_nav_menu_home_link( $items, $args ) {
    $home_link = '';
    if ( INSERT CONDITIONALS HERE ) {
        // Determine menu item class
        $home_link_class = ( is_front_page() ? ' class="current-menu-item"' : '' );
        // Build home link markup
        $home_link = '<li' . $home_link_class . '>';
        $home_link .= $args->before;
        $home_link .= '<a href="' . home_url() . '">';
        $home_link .= $args->link_before . 'Home' . $args->link_after;
        $home_link .= '</a>';
        $home_link .= $args->after;
        $home_link .= '</li>';
    }
    // Merge home link menu item with nav menu items
    $modified_items = $home_link . $items;
    // Return the result
    return $modified_items;
}
add_filter( 'wp_nav_menu_items', 'wpse45802_add_nav_menu_home_link', 10, 2 );
?>

If you need to limit the filter further, perhaps to filter only certain theme_location outputs, let me know, and I'll update. Also, if you can describe your actual conditionals, I'll add those as well.

share|improve this answer
    
Thanks, that's what I ended up using. You have to add one thing though. If condition is false, then you have to return $items, otherwise no menu items are returned. –  Steven Mar 16 '12 at 20:21
1  
Actually, the code above should already return $items. All it does is prepend $items with either home link markup or an empty string, before returning. Is it not doing so? –  Chip Bennett Mar 16 '12 at 20:23
    
You are right. In my solution I didn't have the variable outside the if statement so I can concatenate the additional menu item. That's why it first return zero menu items. –  Steven Mar 16 '12 at 20:27
    
Cool; I wanted to make sure. I always try to make it a habit never to modify the passed parameter directly, but only make changes in a separate variable, and then return the modified result. But, I wanted to make sure I didn't have a syntax error or something. :) –  Chip Bennett Mar 16 '12 at 21:16
add comment

You can get menus using theme_location parameter, see codex page for more information.

This is how Twenty Eleven gets menu using this method - http://core.trac.wordpress.org/browser/trunk/wp-content/themes/twentyeleven/header.php#L118

share|improve this answer
    
wp_nav_menu is what I'm using now. But now I have to add 'home' menu item depending on certain criteria. –  Steven Mar 16 '12 at 16:45
    
Would it not perhaps be more beneficial to re-phrase your question to ask what you're ultimately trying to accomplish - that is, how to add "home" menu item conditionally to custom menus? –  Chip Bennett Mar 16 '12 at 16:50
add comment

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

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

more about that function: http://codex.wordpress.org/Function_Reference/wp_nav_menu

share|improve this answer
    
wp_nav_menu is what I'm using now. But now I have to add 'home' menu item depending on certain criteria. –  Steven Mar 16 '12 at 16:41
add comment

You can use wp_get_nav_menu_object to get the nave menu by ID, slug or name (matches in that order).

 $menu_obj =wp_get_nav_menu_object($id_slug_or_name);

To get the menu object by theme location you can use this function (see related question)

 function wpse45700_get_menu_by_location( $location ) {
  if( empty($location) ) return false;

  $locations = get_nav_menu_locations();
  if( ! isset( $locations[$location] ) ) return false;

  $menu_obj = get_term( $locations[$location], 'nav_menu' );

  return $menu_obj;
}

Stored in the menu object $menu_obj; is the ID:$menu_obj->ID;

Of course, if you are simply displaying the menu, as the other two answers have said you can use wp_nav_menu which accepts ID, slug or name (matches in that order)

share|improve this answer
    
Will I be able to add menu item to menu_obj? –  Steven Mar 16 '12 at 17:02
add comment

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.