0

My Secondary Navigation Menu works fine until I use add_filter( 'wp_nav_menu_items') to add a login / logout button to my primary menu. Here is my code:

add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
 function add_loginout_link( $items, $args ) {
//Add item to primary menu
if ($args->theme_location == 'primary') {

    //checks if user is logged in
   if (is_user_logged_in()) {
       //builds menu item and sub items
    $items .= '<li id="menu-item-portal" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children">'.
   // $submenus .
    //Sub Items
    '<ul class="sub-menu">' .
    //FAQ
        '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-232">'.
            '<a href="' . get_site_url() . '/portal/Faq">'.
                '<span>FAQ</span>' .
            '</a>'.
        '</li>'.
        // Contacts
        '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-232">'.
            '<a href="' . get_site_url() . '/portal/Contacts">'.
                '<span>TeAHN Contacts</span>' .
            '</a>'.
        '</li>'.
        //Bulletin
         '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-232">'.
            '<a href="'. get_site_url() . '/portal/Bulletin">'.
                '<span>Bulletin</span>' .
            '</a>'.
        '</li>'.
        // Documents
          '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-232">'.
            '<a href="'. get_site_url() . '/portal/Documents">'.
                '<span>Documents</span>' .
            '</a>'.
        '</li>'.
        //Logout
        '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-232">'.
            '<a href="'. wp_logout_url() .'">'.
                '<span>Log Out</span>' .
            '</a>'.
        '</li>'.

    '</ul>'.



    '<a href="'.  get_site_url() . '/portal">Portal</a></li>';
}
elseif (!is_user_logged_in()) {
    $items .= '<li id="menu-item-login"><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
}
return $items;
}


}
1

You need to move return $items; outside of the if check. Currently, you are only returning menu items when the theme location is primary, all other menus will have no menu items if you return nothing for those.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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