An Even Better Author List in WordPress
by c.bavota | Posted in Tutorials | 37 comments
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 '<ul class="contributors">';
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 '<li><a href="', $author_profile_url, '">', $avatar , '</a><a href="', $author_profile_url, '" class="contributor-link">', $display_name, '</a></li>';
}
echo '</ul>';
If we put all the code together we get the following:
<?php
$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
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;
}
echo '<ul class="contributors">';
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 '<li><a href="', $author_profile_url, '">', $avatar , '</a><a href="', $author_profile_url, '" class="contributor-link">', $display_name, '</a></li>';
}
echo '</ul>';
?>
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;



Hi, could you tell me how to get this to work with the userphoto plugin and possibly display author descriptions as well? thanks!
The author description is contained in
$author['description'];so you can place something like this in the foreach:Then you display it by replacing the echo code with this:
As for using the user photo plugin, you can replace the avatar code with the code they provide:
If that doesn’t work check out the plugin page: http://wordpress.org/extend/plugins/user-photo/
Thank you very much for your help!
The photo didn’t show up at first, but I finally managed to make it happen with:
Nice shot! could you help me to use the said, userphoto? When I tried it, the photo didn’t show up. May be, I need more techniques for the success of my work, hoping for your consideration on this, thanks
Hello Mr. Bavota, Thanks for the tips you give on this page. Always happy to look at your site. I’m not good at CSS but I am learning. So I can also transpose this. Thank you
Hey buddy, how do i add the number of posts inside the query?
You can set the
numberparameter in theget_user()function. Read about it here:http://codex.wordpress.org/Function_Reference/get_users
Thanks for your helpful information I will try it now…
Nice info. Thanx 4 sharing.
Love this way to create an Index page for the authors! I’m also using Simple CRM by Stats to add additional fields to my user profiles ( like titles and phone numbers).
Inside my profile page I use to pull back and display the title. How would i alter the foreach get and the display to show these?
Sorry… the code i used got stripped out of the comment… but its ?php the_author_meta( ‘title’ ); ?
Hi, this is just what I have been looking for. All works great except for one thing; I just get the default wordpress avatar displaying for all authors instead of thier unique gravatar.com gravatar. What do i need to change please in order to get this working. It’s at the top of the sidebar on this url: http://klood.com/blog
Thankyou
Daniel
The Gravatar is based off of the users email address so you need to make sure that the email address on the profile is actually used in their Gravatar account.
Following on from that, don’t make the mistake I did a few years ago and start signing up for a new gravatar account for every email you have. Massive waste of time. Felt quite stupid when I realised my mistake
You made my day!
Very nice. Also, on a related note, don’t forget to tie in the author pages with google’s new author markup. It’s hit and miss whether you succeed in getting the addition at the moment but I’m guessing it’ll get a full release over the next few months…
Such a huge help, thanks!
I’ve been working on my wordpress theme trying to accommodate my guest bloggers. I have seen other blogs with an avatar in the authors section and I am going to try to set this up by me. Thanks for the inspiration.
Hi all,
First, I thank you very much for this code, it’s exactly what I was looking for!
I just got a problem with it on my page: http://radiogmt.com/asso/membres/. I’d like the author name to be displayed just below his photo, but it doesn’t, I don’t understand why.
Do you think it is a PHP or a CSS problem?
Ty in advance guys!
They changes the array in WordPress 3.3 so you need to use this to display the name:
Of course, I should have thought about it!
Thank you very much for your help and for this beautiful code!
Thanks for this tutorial. We are currently experimenting with different authors in our wordpress, so this will be very useful. Merry Christmas to all.
Our business blog currently has many authors. We have been wanting to develop avatars for them for awhile now. I haven’t found a great tutorial that allows me to do this very easily within WordPress. It is nice to know that you have found a better way of collecting and displaying all the information. I also like that you have more control over the output.
Many Thanks Mr. Bavota, excellent description. I’m Glad I found your your site. With your help, I’m learning something new every time I visit your site. I gladly Thank You.
Thanks Mr. Bavota, your site is like a treasure for newbie developers like us, will keep track of your posts. Thanks a lot again. God bless you!!!
Thank you for the above code… I hope that it is open source because I want to use it on my future projects. Finally I understand how to do it.
would be cool to list all attachments uploaded by each author
This is so helpful! I used this on a project a few months ago, but with the 3.3 update, the display_name and description stopped showing up. I followed your tip about changing the array for display_name, but could you what it should be for description now? I can’t seem to figure that out.
Thanks so much!
Hi,
there is a way to display this list of our authors with this code allowing to paginate the output? per example 50 per page. I wonder that because i have from 200 to 300 authors in my blog.
If affirmative what part of the code need to modify?
many thanks.
That is probably doable but not really as a widget, unless you hooked into using Ajax.
The author description no longer shows up in 3.3 for me. (I was using the code in the first comment.) Can you tell me how I can rewrite it like with display_name? Thanks.
I just added a little note above about the change required for WordPress 3.3.
Sorry to be asking again, but the problem I’m having is with this line…
How should this line be rewritten in 3.3?
The chance would be similar to the note above:
I’ve tried changing it like that, and it’s still not showing up.
http://www.ctpubblog.com/blog-authors/
echo ''; foreach($authors as $author) { $display_name = $author['data']->display_name; $description = $author['data']->description; $avatar = get_avatar($author['ID'], $avatar_size); $author_profile_url = get_author_posts_url($author['ID']); echo ''.$display_name.'<a href="', $author_profile_url, '">', $avatar , '</a>'.$description.'<a href="', $author_profile_url, '">➤ Posts by '.$display_name.'</a>'; } echo '';Nevermind! I found the answer. This works: