5

I'm working on creating some text that says 'Login' to users that are not logged in, and the user's username or display name when logged in.

It seems like it should be an easy problem to solve, and I've found the following two bits of code on the wordpress codex that each do half of what I am looking for, but I haven't figured out how to combine them (without breaking the site).

Is this the correct direction, or way off base?

To check if the user is logged in and display something different depending:

<?php if ( is_user_logged_in() ) {
    echo '{username code here}';
} else {
    echo 'Login';
}
?>

To get and display the current user's information:

<?php global $current_user;
wp_get_current_user();
echo 'Username: ' . $current_user->user_login . "\n";
echo 'User display name: ' . $current_user->display_name . "\n";
?>
15

This seems to do what you need.

<?php global $current_user; wp_get_current_user(); ?>
<?php if ( is_user_logged_in() ) { 
 echo 'Username: ' . $current_user->user_login . "\n"; echo 'User display name: ' . $current_user->display_name . "\n"; } 
else { wp_loginout(); } ?>

protected by Community Oct 15 '17 at 11:33

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.