Support » Fixing WordPress » How do I display "Welcome, (Username)" for logged in users?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Yes, it is possible. However it requires basic knowledge of PHP. Trying to guide you in the right direction, you would need to use a function such as http://codex.wordpress.org/Function_Reference/is_user_logged_in

    Good luck and have fun 🙂

    Hey Keith,

    Thanks for the response mate, much appreciated.

    Could you tell me how I go about changing the top menus depending on whether a user was logged in or not?

    Ok, so I think I might be getting somewhere. Kinda.
    I entered the following code in my functions.php:

    add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
    function my_custom_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 style="padding: 1.37em 1em; font-family: bree serif; color: rgb(159, 150, 147);">Welcome, '.$name.'';
        }
        return $items;
    }

    which does indeed display “Welcome, User”. However, it displays on both my top menu and my main nav menu.

    What would I need to change to get it to display only on the top menu?

    Thanks in advance.

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    You can test for the menu’s theme_location like this:

    add_filter( 'wp_nav_menu_items', 'my_custom_menu_item', 10, 2 );
    function my_custom_menu_item( $items, $args ) {
    
    	if ( isset( $args ) && $args->theme_location === 'primary' ) {
    		// your stuff here
    	}
    	return $items;
    }

    Or use the “wp_nav_menu_{$menu->slug}_items” filter hook. Example with ‘top-menu’ menu slug.

    add_filter( 'wp_nav_menu_top-menu_items', 'my_custom_menu_item', 10, 2 );
    function my_custom_menu_item( $items, $args ) {
    	// your stuff here
    	return $items;
    }

    hi there … behind whats following; I AM A ROOKIE!

    i tried to get displayed “Welcome Firstname Lastname”. Firstname (in this case (“Mr” “Title”) is OK but “Lastname” has a “more” tag and klicking on this is displaying the “Lastname” …

    +++++++++++++++++++++++++

    add_filter( ‘wp_nav_menu_items’, ‘my_custom_menu_item’);
    function my_custom_menu_item($items)
    {
    if(is_user_logged_in())
    {
    $user=wp_get_current_user();
    $name=$user->user_firstname; // or user_login , user_firstname, user_lastname, display_name
    $name2=$user->user_lastname; // or user_login , user_firstname, user_lastname, display_name
    $items .= ‘<li style=”padding: 1.37em 1em; font-family: inherit; color: #FFAD00;”>Guten Tag, ‘.$name.”;
    $items .= ‘<li style=”padding: 1.37em 1em; font-family: inherit; color: #FFAD00;”>’.$name2.”;

    }
    return $items;
    }
    ++++++++++++++++
    Screenshot here

    Please be so kind and gimme a little help 🙂

    Cheers joerg

    Fixt it :-9 Code is OK but Menu space was tooo small …

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How do I display "Welcome, (Username)" for logged in users?’ is closed to new replies.