How to Display a List of WordPress Authors with Avatars and Usernames
Heres a little tip on how to display a list of WordPress authors with avatars and username.
Instructions
Add this code anywhere in your 'templates'
file.
<?php $display_admins = false; $order_by = 'post_count'; // 'nicename', 'email', 'url', 'registered', 'display_name', or 'post_count' $order = 'DESC'; $role = ''; // 'subscriber', 'contributor', 'editor', 'author' - leave blank for 'all' $avatar_size = 161; $hide_empty = false; // hides authors with zero posts 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.'&order='.$order.'&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 '<ul id="grid-contributors">'; foreach($authors as $author) { $display_name = $author['data']->display_name; $avatar = get_avatar($author['ID'], $avatar_size); $author_profile_url = get_author_posts_url($author['ID']); echo '<li class="single-item">'; echo '<div class="author-gravatar"><a href="', $author_profile_url, '">', $avatar , '</a></div>'; echo '<div class="author-name"><a href="', $author_profile_url, '" class="contributor-link">', $display_name, '</a></div>'; echo '</li>'; } echo '</ul>'; ?>
I’m a little new to WP. How would I go about showing multiple roles? Like the admin and authors? It doesn’t show anything when I try the following:
$role = ‘author, inbound_marketer, admin’;
Also, how do I get it to show other user info? Like number of posts? member since date, or their social links?