I am trying to show the name of the currently logged-in user in a small text like 'Hello, username!'.

I tried to do this, but it doesn't work:

<p class="vam">
    <?php if(is_user_logged_in()):
    $current_user = wp_get_current_user(); ?>
        Hello, <b><?php echo $current_user->display_name; ?></b>!
    <?php else:?>
        Hello, <span>Client</span>!`
  • 2
    Your code in the question body is missing the <?php endif; ?> and </p> - are they just typo in the question? But other than that, your code is good, so in what template/file you put the code? Did you get any error?
    – Sally CJ
    Oct 12 '20 at 18:49

try my variant:

<?php if (!is_user_logged_in()) : ?>
    <form action="<?=get_home_url(); ?>myaccount" method="POST">                                            
        <input type="submit" value="<?php _e('Hello, Client! Click to sign in:', 'theme'); ?>">                     
<?php  else:  ?>
    <a href="<?=get_home_url();?>myaccount"><?php _e('Hello ', 'theme'); echo wp_get_current_user()->user_login;?></a>
    <a href="<?=wp_logout_url(esc_url( 'https://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ));?>myaccount">Logout</a>
<?php  endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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