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.

I have the following lines of code in my functions.php file:

#-----------------------------------------------------------------#
# Custom menu
#-----------------------------------------------------------------#
if ( function_exists( 'register_nav_menus' ) ) {
    register_nav_menus(
        array(
          'top_nav' => 'Top Navigation Menu',
          'secondary_nav' => 'Secondary Navigation Menu <br /> <small>Will only display if applicable header layout is selected <a href="'. admin_url('?page=redux_options&tab=4') .'">here</a>.</small>'
        )
    );
}   


//dropdown arrows
class Theme_Arrow_Walker_Nav_Menu extends Walker_Nav_Menu {
    function display_element($element, &$children_elements, $max_depth, $depth=0, $args, &$output) {
        $id_field = $this->db_fields['id'];
        if (!empty($children_elements[$element->$id_field]) && $element->menu_item_parent == 0) { 
            $element->title =  $element->title . '<span class="sf-sub-indicator"><i class="icon-angle-down"></i></span>'; 
            $element->classes[] = 'sf-with-ul';
        }

        if (!empty($children_elements[$element->$id_field]) && $element->menu_item_parent != 0) { 
            $element->title =  $element->title . '<span class="sf-sub-indicator"><i class="icon-angle-right"></i></span>'; 
        }

        Walker_Nav_Menu::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
    }
}

And the following lines of code in my header.php file:

<?php if(has_nav_menu('secondary_nav')) { ?>
    <ul class="sf-menu">
        <?php wp_nav_menu( array('walker' => new Theme_Arrow_Walker_Nav_Menu, 'theme_location' => 'secondary_nav', 'container' => '', 'items_wrap' => '%3$s' ) ); ?>
    </ul>
<?php } ?>

Which displays a primary and more importantly regarding this issue, a secondary navigation menu:

enter image description here

enter image description here

I would like to add a login navigation hyperlink, just below the My Account hyperlink, and remove it if the user is logged-in.

When adding this using the Admin > Appearance > Menus settings...

enter image description here

It leaves the login navigation hyperlink there, even if the user is logged-in.

How would I be able to add a login navigation hyperlink to my secondary navigation menu, and remove it from the list once the user is logged in to the website, and if possible without an additional plugin?

Thank you.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You could do this with a filter on wp_get_nav_menu_items.

In this example, we first check if it's the admin screen or the user is not logged in, and bail out if that's the case.

Then we look at each menu item and find the one with Log In as the menu item title, and unset it.

function wpa_remove_menu_item( $items, $menu, $args ) {
    if( is_admin() || ! is_user_logged_in() ) return $items;

    foreach ( $items as $key => $item ) {
        if ( 'Log In' == $item->title ) unset( $items[$key] );
    }
    return $items;
}
add_filter( 'wp_get_nav_menu_items', 'wpa_remove_menu_item', 10, 3 );
share|improve this answer
    
Hi there Milo. I'm not too sure if I've done something wrong, but this doesn't seem to add a Log In hyperlink just below the My Account hyperlink. I basically need a Log In hyperlink to appear when the user is not logged in, and for it to then be removed when they are. –  user1752759 Mar 7 at 2:28
    
This doesn't add menu items, it removes an item you've added to the menu. –  Milo Mar 7 at 3:00
    
Yes, I understand this. I would like to add a login navigation hyperlink, just below the My Account hyperlink, and remove it if the user is logged-in. –  user1752759 Mar 7 at 3:15
1  
Go to appearance > menus and under Links, add a link to the login page. –  Milo Mar 7 at 3:19
    
Ahhhh...Gotcha Milo. It must be frustrating working with people like me. –  user1752759 Mar 7 at 3:35

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.