WordPress.org

Forums

[resolved] Display current username in conditional function (5 posts)

  1. Florence
    Member
    Posted 2 years ago #

    Hi! I'm fairly new to coding and I'm having trouble combining 2 funtions. Here's what I've got:

    <?php
    if ( is_user_logged_in() ) {
        echo 'Welcome, registered user!';
    } else {
        echo 'Welcome, visitor!';
    };
    ?>

    Instead of "registered user, I'd like it to display the current username.

    This is what I had:

    <?php global $user_identity ;
          get_currentuserinfo();
    
          echo $user_identity ;
    ?>

    I've tried several combinations but I can't seem to figure it out.

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    have you read the Codex http://codex.wordpress.org/Function_Reference/get_currentuserinfo

    and what of the information have you tried to use?

  3. Florence
    Member
    Posted 2 years ago #

    Woah that's a quick reply! Yes I have. That's where I got the bottom snippet from, actually. I can get the username to display using that function, but I can't get it to display within the conditional function (the top snippet). When I try to combine them I get a blank page. I just don't know what I'm doing, I guess :)

  4. alchymyth
    Forum Moderator
    Posted 2 years ago #

    try this combination:

    <?php
    if ( is_user_logged_in() ) {
        global $current_user;
        get_currentuserinfo();
        echo 'Welcome, ' . $current_user->user_login . '!';
    } else {
        echo 'Welcome, visitor!';
    };
    ?>
  5. Florence
    Member
    Posted 2 years ago #

    Thank you! That worked perfectly!

Topic Closed

This topic has been closed to new replies.

About this Topic