I already wrote about this a while back in “How to Display an Author List with Avatars in WordPress” but I recently realized that it needed updating. There’s a better way of collecting and displaying all the information and you can also add a few variables to give you more control over the output.
Let’s start off with creating our variables:
$display_admins = false; $order_by = 'display_name'; // 'nicename', 'email', 'url', 'registered', 'display_name', or 'post_count' $role = ''; // 'subscriber', 'contributor', 'editor', 'author' - leave blank for 'all' $avatar_size = 32; $hide_empty = true; // hides authors with zero posts
Next we can add the code to gather our contributors:
if(!empty($display_admins)) { $blogusers = get_users('orderby='.$order_by.'&role='.$role); } else { $admins = get_users('role=administrator'); $exclude = array(); foreach($admins as $ad) { $exclude[] = $ad->ID; } $exclude = implode(',', $exclude); $blogusers = get_users('exclude='.$exclude.'&orderby='.$order_by.'&role='.$role); } $authors = array(); foreach ($blogusers as $bloguser) { $user = get_userdata($bloguser->ID); if(!empty($hide_empty)) { $numposts = count_user_posts($user->ID); if($numposts < 1) continue; } $authors[] = (array) $user; }
Now that we have all the information we need, let's display it:
echo '
- ';
foreach($authors as $author) {
$display_name = $author['display_name'];
$avatar = get_avatar($author['ID'], $avatar_size);
$author_profile_url = get_author_posts_url($author['ID']);
echo '
- ', $avatar , '', $display_name, ' '; } echo '
If we put all the code together we get the following:
ID; } $exclude = implode(',', $exclude); $blogusers = get_users('exclude='.$exclude.'&orderby='.$order_by.'&role='.$role); } $authors = array(); foreach ($blogusers as $bloguser) { $user = get_userdata($bloguser->ID); if(!empty($hide_empty)) { $numposts = count_user_posts($user->ID); if($numposts < 1) continue; } $authors[] = (array) $user; } echo '
- ';
foreach($authors as $author) {
$display_name = $author['display_name'];
$avatar = get_avatar($author['ID'], $avatar_size);
$author_profile_url = get_author_posts_url($author['ID']);
echo '
- ', $avatar , '', $display_name, ' '; } echo '
All it needs is a little CSS to style it:
.contributors .avatar { float: left; clear: both; background: #fff; padding: 2px; border: 1px solid #ccc; border-radius: 3px; -moz-border-radius: 3px; -webkit-border-radius: 3px; box-sizing: content-box; -moz-box-sizing: content-box; -webkit-box-sizing: content-box; } .contributors .avatar:hover { border-color: #999; } .contributors .contributor-link { float: left; margin: 10px 0 0 10px; }
You can play around with the CSS to get it looking the way you want and with the variables in place, you can control exactly what type of author list will be displayed.
NOTE: The folks at WordPress decided to change the array setup in version 3.3 so now you need to change:
$display_name = $author['display_name'];
To the following in order to display the author name:
$display_name = $author['data']->display_name;