WordPress.org

Forums

[resolved] Move login/logout to beginning of menu? (12 posts)

  1. rwilki
    Member
    Posted 1 year ago #

    I've been struggling with this and can't figure out an answer.

    I'm using this code to add a login/logout url to one of my menus. But I'd like it to be at the beginning of the menu, not the end. I'm pretty sure it has to do with the priority of "10,2" but that's all I got. Can anyone help?

    Thanks,
    Bob

    add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
    function add_loginout_link( $items, $args ) {
        if (is_user_logged_in() && $args->theme_location == 'secondary') {
            $items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>';
        }
        elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
            $items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
        }
        return $items;
    }
  2. Andrew
    Forum moderator
    Posted 1 year ago #

    Can you float the menu right instead, using CSS?

  3. rwilki
    Member
    Posted 1 year ago #

    thanks Andrew. I wish I could use css, but the menu is already floated:right and I don't think that'll work. for some reason, it's still adding my new menu item (login) after the secondary menu. I'm guessing that's because of a higher priority?

    thanks,
    Bob

  4. Andrew
    Forum moderator
    Posted 1 year ago #

    Are you working locally? If not, may you link the site?

  5. rwilki
    Member
    Posted 1 year ago #

    Thanks Andrew. Just sent you an email from your website.
    Bob

  6. Andrew
    Forum moderator
    Posted 1 year ago #

    Is this what you're trying to do http://awesomescreenshot.com/0ea174b810 ?

  7. rwilki
    Member
    Posted 1 year ago #

    absolutely! I'm afraid absolute positioning won't work because the login link changes depending upon your status. maybe you have another idea?

    thanks,
    bob

  8. Andrew
    Forum moderator
    Posted 1 year ago #

    Can you add this CSS;

    .memby {
     float: left;
    }
    
    #top-menu li {
     margin-right: 10px;
     padding-right: 10px;
     margin-left: 0;
     padding-left: 0;
     border-left: 0;
     border-right: solid 1px #f1f1f1;
    }
  9. Andrew
    Forum moderator
    Posted 1 year ago #

    There was something else to add... 1 sec

  10. rwilki
    Member
    Posted 1 year ago #

    no worries... thanks so much for your help.

    I think I got it with the following:

    .memby {
    float: left;}
    
    #top-menu li {
    display:inline;
    margin-right: 10px;
    padding-right: 10px;
    margin-left: 0;
    border-right:1px solid #ccc;
    padding-left: 0;}
    
    #top-menu li:first-child {padding-left: 0;}
    #top-menu li:last-child {border:none;padding-right:0;margin-right:0;}
  11. Andrew
    Forum moderator
    Posted 1 year ago #

    Good :)

  12. rwilki
    Member
    Posted 1 year ago #

    you are awesome Andrew thanks!!!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags