1

Is it possible to change the username to nickname in the comments section?

Here's the code I currently use to show the user's username in comment section:

    <?php
    /* translators: 1: comment author, 2: date and time */
        printf( '%1$s  %2$s ',
            sprintf( '<span>%s</span>', get_comment_author_link() ),

            sprintf( '',
                esc_url( get_comment_link( $comment->comment_ID ) ),
                get_comment_time( 'c' ),
                /* translators: 1: date, 2: time */
                sprintf( '%1$s ، %2$s', get_comment_date("d M Y"), get_comment_time() )
                ));
            ?>

I tried using below code instead of the above code to show the nickname:

<?php
    $commenter_id = $comment->user_id;
    $commenter_name =  get_usermeta($commenter_id,'nickname');?>
<?php echo $commenter_name; ?>  

But the problem is after posting a comment by a guest user, it doesn't show the name he added before posting comment.

Thanks.

3

Yes, of course it won't show any name for guest comments because they are not registered users of your website so they don't have any nickname associated.

What you can do in this situation, is add a check before printing the nickname, if comment was made by a user or a guest. And act accordingly.

So this is the code you want to use instead.

if ( $comment->user_id != '0' ) {
    echo '<span class="some-class">' . get_user_meta( $comment->user_id, 'nickname', true ) . '</span>';
} else {
    echo '<span class="other-class">' . get_comment_author_link() . '</span>';
}

This checks if commenter user_id is 0 (for guests) then it will show guest name with link url (default) and if it is a user then it will print user's nickname.

  • Thank you so so much, Robert. But how can I add a class to second or forth line for styling? – Kevin Armstrong Dec 23 '14 at 20:39
  • @KevinArmstrong I have updated code and added span classes for each. Please check. – Robert hue Dec 23 '14 at 20:48

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.