An Even Better Author List in WordPress

Posted In CMS, Tips, Tools, Web Development, Wordpress - By technologystory On Friday, September 30th, 2011 With 0 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 '
;

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']);

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>