Okay
  Public Ticket #478180
Creating dynamic Log In / Log Out links in navigation
Closed

Comments

  • Richard started the conversation

    Hi there,

    I am using this WooCommerce tutorial to attempt to add Log In / Log Out links to my primary navigation depending on whether the customer is logged in or out.

    https://support.woothemes.com/hc/en-us/articles/203106357-Add-Login-Logout-Links-To-The-Custom-Primary-Menu-Area

    However, the code they supply there doesn't work. Any idea how I can get this working in your theme?

    So for example, if a user is logged out, they should say the words Log In in the primary navigation. If they are logged in, they should see the words Log Out. Each set of words should link to the correct WooCommerce log in / log out functions.

    Let me know your thoughts,


    Richard


  • Richard replied

    This is fixed. Used the following code in functions.php:

    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 == \'top_nav\') {
            $items .= \'<li><a href=\"\'. site_url(\'my-account\') .\'\">MY ACCOUNT</a></li>\';
            $items .= \'<li><a href=\"\'. site_url(\'my-account/customer-logout\') .\'\">LOG OUT</a></li>\';
        }
        elseif (!is_user_logged_in() && $args->theme_location == \'top_nav\') {
            $items .= \'<li><a href=\"\'. site_url(\'my-account\') .\'\">LOG IN / REGISTER</a></li>\';
        }
        return $items;
    }