how do I add “home” menu item conditionally to custom menus?

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.

Read More

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 😉

Related posts

Leave a Reply

4 comments

  1. 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.

  2. 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

  3. 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)