• How can I make the Welcome, Visitor/User to appear only on the top bar menu?

    function top_menu_item($items)
    {
    if(is_user_logged_in())
    {
    $user=wp_get_current_user();
    $name=$user->display_name; // or user_login , user_firstname, user_lastname
    return ‘

    • Welcome, ‘.$name.’
    • ‘ . $items;
      }
      else
      {
      return ‘

    • Welcome, Visitor
    • ‘ . $items;
      }
      //return $items;
      }
      add_filter( ‘wp_nav_menu_items’, ‘top_menu_item’);

      With the function above, the Welcome Visitor/User appears on all navigation menus, top bar menu, primary menu, footer bar menu and other navigation menu I add on widgets. I want it to appear only on top bar menu.

Viewing 3 replies - 1 through 3 (of 3 total)
  • function top_menu_item( $items ) {
        if(is_user_logged_in())
        {
            $user=wp_get_current_user();
    	$name=$user->display_name; // or user_login , user_firstname, user_lastname
    	$items .= '<li><a href="">Welcome '.$name.'</a></li>';
        } else {
    	$items .= '<li><a href="">Welcome Visitor</a></li>';
        }
    
        return $items;
    }
    
    add_filter( 'wp_nav_menu_items', 'top_menu_item');

    You have extra return statement which prevents the function to return filtered menu items.

    Please make sure you format your code properly before submitting!
    Thank you.

    Thread Starter mcricocab

    (@mcricocab)

    Hello, sorry about the format of the code. And, thank you for the reply.

    It just moved Welcome, Visitor/User to the right side of the lists. It still appears on top bar menu, primary menu, footbar menu and widget.

    • This reply was modified 6 years, 6 months ago by mcricocab.
    Thread Starter mcricocab

    (@mcricocab)

    The code will put welcome user/visitor to every menu I have on my website. I found this demo site of avant https://demo.kairaweb.com/#avant. As you can see, there is a text there on the top bar menu, “A leaf”. I’m gonna change that text to Welcome plus the username when logged in.

    If I inspect the code:
    <div class=”site-top-bar-right-extra-txt”>
    A leaf </div>

    How can I add that (change it to Welcome) with the username when logged in and just input Welcome, Visitor when logged out?

    • This reply was modified 6 years, 6 months ago by mcricocab.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Welcome, (Visitor/User) on Top Bar Menu only’ is closed to new replies.