• Resolved Mamphey

    (@mamphey)


    Hi there,

    I found a login / logout url code on this site which i placed in my functions.php

    The code worked fine except that it places the login logout link in two places on my site. I have a top menu and a main menu. Ideally i would like the link showing only for the top menu.

    Is there a way to do that? Please find the code below. My website address is http://www.ghanalandlords.com/

    Thank you

    add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
    function add_login_logout_link($items, $args) {
            ob_start();
            wp_loginout('index.php');
            $loginoutlink = ob_get_contents();
            ob_end_clean();
            $items .= '
    
    <li>'. $loginoutlink .'</li>
    ';
        return $items;
    }

    [In future please wrap code in backticks or using the “code” button]

Viewing 3 replies - 1 through 3 (of 3 total)
  • The problem is your hooking into every instance of “wp_nav_menu_items” .. You need to only target 1 menu. Try the below code instead of the one you have? If it doesn’t work.. can you tell me what your menu slug is

    add_filter('wp_nav_menu_ghanalandlords_items', 'add_login_logout_link', 10, 2);
    function add_login_logout_link($items, $args) {
            ob_start();
            wp_loginout('index.php');
            $loginoutlink = ob_get_contents();
            ob_end_clean();
            $items .= '
    
    <li>'. $loginoutlink .'</li>
    ';
        return $items;
    }

    You could also use a plugin called BAW Login/Logout menu. With this plugin you can add a log in/logout item menu with autoswitch when user is logged in or not. Of course, if you decide to use this plugin, remove the filter you mentioned above from functions.php

    Thread Starter Mamphey

    (@mamphey)

    crgeary

    your suggestion worked like a charm. Just wanna say thank you very very very much.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Login / logout url’ is closed to new replies.