List Author Information

Several persons have requested code to list information about Post authors. The sample code below illustrates one technique.

<?php

// Print author information ordered by number of posts DESC, then name
function sort_authors_by_numposts($a,$b) {
   $adata = $a['data'];
   $bdata = $b['data'];
   if ($adata->numposts == $bdata->numposts) {
      $aname = strtolower($adata->display_name);
      $bname = strtolower($bdata->display_name);
      if ( $aname == $bname ) {
         return 0;
      } else {
         return ( $aname < $bname) ? -1 : 1;
      }
   } else {
      return ($adata->numposts < $bdata->numposts) ? 1 : -1;
   }
}

$avatar_size = 156;  // Avatar size in pixels
$hide_empty = false;  // Don't display authors with no posts if true
$hide_admin = false;  // Don't display administrators if true
$sort_by_number_posts = false;  // Sort by display name if false

$authors = array();
$blogusers = get_users('orderby=displayname');
foreach ($blogusers as $bloguser) {
	$user = get_userdata($bloguser->ID);
	$numposts = count_user_posts($user->ID);
	if($hide_empty) {
		if(!$numposts) continue;
	}
   $user->numposts = $numposts;
	$authors[] = (array) $user;
}
echo '<ul>';
if($sort_by_number_posts) uasort($authors,'sort_authors_by_numposts');
foreach($authors as $author_data) {
   $author = $author_data['data'];
   $caps = array_keys(get_userdata($author->ID)->caps);
   $is_admin = (in_array('administrator',$caps) );
   if($hide_admin && $is_admin) continue;

	$display_name = $author->display_name;
	$description = get_userdata($author->ID)->user_description;
	$avatar = get_avatar($author->ID, $avatar_size);
	$author_profile_url = get_author_posts_url($author->ID);
   $numposts = $author->numposts;

	echo '<li><h3>'.$display_name. " ($numposts)" . '</h3><a href="', $author_profile_url, '">', $avatar , '</a><p>'.$description.'</p><p><a href="', $author_profile_url, '" class="contributor-link">➤ Posts by '.$display_name.'</a></p></li>';
}
echo '</ul>';

?>

Leave a Reply

Your email address will not be published. Required fields are marked *


*

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