2

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>';
    }
?>

2 Answers 2

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>';
}
4

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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