admin管理员组

文章数量:1391955

I'm using this piece of code to list users and their information. The problem I'm having is that the description isn't showing. Obviously it's because the description lays under "user_meta" and not "users". But how would I solve that?

<?php
    $blogusers = get_users('include=2,3,4,5,6,7,8');
    foreach ($blogusers as $user) {
        echo '<li>'
        . get_avatar($user->ID, 120) .
        '<br />'
        . $user->display_name .
        '<br />'
        . $user->user_email .
        '<br />'
        . $user->user_description .
        '</li>';
    }
?>

I'm using this piece of code to list users and their information. The problem I'm having is that the description isn't showing. Obviously it's because the description lays under "user_meta" and not "users". But how would I solve that?

<?php
    $blogusers = get_users('include=2,3,4,5,6,7,8');
    foreach ($blogusers as $user) {
        echo '<li>'
        . get_avatar($user->ID, 120) .
        '<br />'
        . $user->display_name .
        '<br />'
        . $user->user_email .
        '<br />'
        . $user->user_description .
        '</li>';
    }
?>
Share Improve this question edited Oct 5, 2012 at 15:03 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Oct 5, 2012 at 14:41 nicolasnicolas 2652 gold badges3 silver badges8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 7

The Native get_users() function returns an array of user objects and each on holds

   [ID] => 1
   [user_login] => admin
   [user_pass] => $P$Bxudi6gJMk2GRt2ed3xvZ06c1BPZXi/
   [user_nicename] => admin
   [user_email] => [email protected]
   [user_url] => http://localhost/
   [user_registered] => 2010-06-29 07:08:55
   [user_activation_key] => 
   [user_status] => 0
   [display_name] => Richard Branson

as you can see user_description is not a part of this object since it's stored in a different table in the database(usermeta as oppose to users). So Instead of $user->user_description use get_user_meta($user->ID, 'description', true)

<?php
$blogusers = get_users('include=2,3,4,5,6,7,8');
foreach ($blogusers as $user) {
    echo '<li>'
    . get_avatar($user->ID, 120) .
    '<br />'
    . $user->display_name .
    '<br />'
    . $user->user_email .
    '<br />'
    . get_user_meta($user->ID, 'description', true) .
    '</li>';
}

Just in case anyone (just like me) comes across this thread, you can extend this to show custom fields.

Add this to your theme's functions.php:

// Extend user profile
// CUSTOM USER PROFILE FIELDS
function my_custom_userfields( $contactmethods ) {

// ADD CONTACT CUSTOM FIELDS
$contactmethods['contact_phone_office']     = 'Office Phone';
$contactmethods['contact_phone_mobile']     = 'Mobile Phone';

// ADD ADDRESS CUSTOM FIELDS
$contactmethods['address_line_1']       = 'Address Line 1';
$contactmethods['address_line_2']       = 'Address Line 2 (optional)';
$contactmethods['address_city']         = 'City';
$contactmethods['address_state']        = 'State';
$contactmethods['address_postcode']      = 'Postcode';

// ADD SOCIAL CUSTOM FIELDS
$contactmethods['contact_twitter']       = 'Twitter';
$contactmethods['contact_facebook']      = 'Facebook';
$contactmethods['contact_linkedin']      = 'LinkedIn';

return $contactmethods;
}
add_filter('user_contactmethods','my_custom_userfields',10,1);

then just add whatever fields you want to your foreach call, like:

echo '<li>'. get_avatar($user->ID, 120) .'<br />'. $user->display_name .'<br /><a href="mailto:'. $user->user_email .'">'. $user->user_email .'</a><br />'. get_user_meta($user->ID, 'description', true) .'<br />'. get_user_meta($user->ID, 'contact_twitter', true) .' - '. get_user_meta($user->ID, 'contact_facebook', true) .' - '. get_user_meta($user->ID, 'contact_linkedin', true) .'</li>';

I hope this is ok to post here. It's slightly off topic, but hopefully it helps (if not mods, please remove).

本文标签: How to get the user description with getusers