Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am currently calling a user's nicename in a function I have:

   $users = $user_query->get_results();  
   foreach( $users as $user ):    
   echo $user->user_nicename; 

I'm trying to call the user's Nickname instead, but nothing I do is working such as:

    echo $user->user_nickname;

Is there a special way you have to call a user's nickname?

Thanks

share|improve this question
1  
What does $user_query->get_results() return? –  Chip Bennett May 21 '12 at 14:50
add comment

4 Answers 4

If you want to query all users, try using the get_users() function. Note that this function returns an array:

<?php
$users = get_users();

foreach( $users as $user ) {
    echo $user['user_nicename'];
}
?>
share|improve this answer
    
I have a custom code that provides a list of users based on certain criteria, but I am just not able to successfully call (and display) the user's nickname instead of their nicename. –  Teddy May 21 '12 at 14:26
1  
How are we supposed to answer/help, without seeing this "custom code"? –  Chip Bennett May 21 '12 at 14:37
    
I didn't think the code itself was really relevant, I was just asking in general for how to call a user's nickname. –  Teddy May 21 '12 at 14:55
    
Okay, now I'm confused. :) If the code itself isn't really relevant, why did you bring it up? –  Chip Bennett May 21 '12 at 15:41
    
I think I'm confused too... ;) Your original reply mentioned querying all users, which I am not doing. I am just trying to change the way my query displays on my page (using nickname instead of nicename) so that a user's first and last name will appear. –  Teddy May 21 '12 at 16:22
show 1 more comment

Try

echo the_author_meta( 'nickname', $current_user->ID );

or you can also use;

$current_user = wp_get_current_user();
echo $current_user->nickname ;
share|improve this answer
2  
Please explain how to use this with the OP's code. –  s_ha_dum Sep 5 '13 at 13:08
add comment

If you want to display the authors nickname you can simply use the following code inside your loop:

$author->nickname;

When you're trying to display the currently logged in user, you can use:

echo $current_user->nickname;
share|improve this answer
    
Please explain how this would work. Chances are it will take more than the lines of code you posted. –  s_ha_dum Sep 5 '13 at 13:06
    
If you paste the following codes in your theme it would work. It does not take more lines to get it to work. –  Cas van Dongen Sep 5 '13 at 13:23
    
Really? How do you avoid the "undefined variable" and "trying to get a property of a non-object" Notices? –  s_ha_dum Sep 5 '13 at 13:32
add comment

This is impossible to answer without seeing your code but there is no user_nickname in the $wpdb->users table and no user_nickname key in the $wpdb->usermeta table by default.

Assuming that $users is a user object then:

$users = $user_query->get_results();  
foreach( $users as $user ) {  
   echo get_user_meta($user->ID,'nickname',true); 
}

Should get you the user nickname.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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