Skip to content

Instantly share code, notes, and snippets.

@mattwiebe
Created January 23, 2011 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattwiebe/792600 to your computer and use it in GitHub Desktop.
Save mattwiebe/792600 to your computer and use it in GitHub Desktop.
An slight mod of wp_list_authors to optionally add a class to the current author in an author archive.
<?php
/**
* List all the authors of the blog, with several options available.
* Edited to optionally include current-author class on author archives
*
* <ul>
* <li>optioncount (boolean) (false): Show the count in parenthesis next to the
* author's name.</li>
* <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is
* installed bydefault.</li>
* <li>show_fullname (boolean) (false): Show their full names.</li>
* <li>hide_empty (boolean) (true): Don't show authors without any posts.</li>
* <li>feed (string) (''): If isn't empty, show links to author's feeds.</li>
* <li>feed_image (string) (''): If isn't empty, use this image to link to
* feeds.</li>
* <li>echo (boolean) (true): Set to false to return the output, instead of
* echoing.</li>
* <li>style (string) ('list'): Whether to display list of authors in list form
* or as a string.</li>
* <li>html (bool) (true): Whether to list the items in html form or plaintext.
* </li>
* <li>current_author-class (string|boolean) the class to highlight the viewed
* author with on an author archive. False will disable.</li>
* </ul>
*
* @link http://codex.wordpress.org/Template_Tags/wp_list_authors
* @since 1.2.0
* @param array $args The argument array.
* @return null|string The output, if echo is set to false.
*/
function sd_list_authors($args = '') {
global $wpdb;
$defaults = array(
'orderby' => 'name', 'order' => 'ASC', 'number' => '',
'optioncount' => false, 'exclude_admin' => true,
'show_fullname' => false, 'hide_empty' => true,
'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
'style' => 'list', 'html' => true, 'current_author_class' => 'current-author'
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
$return = '';
$query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number' ) );
$query_args['fields'] = 'ids';
$authors = get_users( $query_args );
$current_author_id = ( $current_author_class && is_author() ) ? get_user_by('slug', get_query_var('author_name'))->ID : 0;
$author_count = array();
foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row )
$author_count[$row->post_author] = $row->count;
foreach ( $authors as $author_id ) {
$author = get_userdata( $author_id );
if ( $exclude_admin && 'admin' == $author->display_name )
continue;
$posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
if ( !$posts && $hide_empty )
continue;
$link = '';
if ( $show_fullname && $author->first_name && $author->last_name )
$name = "$author->first_name $author->last_name";
else
$name = $author->display_name;
if ( !$html ) {
$return .= $name . ', ';
continue; // No need to go further to process HTML.
}
if ( 'list' == $style ) {
// add a class if appropriate
if ( $author_id == $current_author_id )
$return .= '<li class="' . $current_author_class . '">';
else
$return .= '<li>';
}
$link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
if ( !empty( $feed_image ) || !empty( $feed ) ) {
$link .= ' ';
if ( empty( $feed_image ) ) {
$link .= '(';
}
$link .= '<a href="' . get_author_feed_link( $author->ID ) . '"';
$alt = $title = '';
if ( !empty( $feed ) ) {
$title = ' title="' . esc_attr( $feed ) . '"';
$alt = ' alt="' . esc_attr( $feed ) . '"';
$name = $feed;
$link .= $title;
}
$link .= '>';
if ( !empty( $feed_image ) )
$link .= '<img src="' . esc_url( $feed_image ) . '" style="border: none;"' . $alt . $title . ' />';
else
$link .= $name;
$link .= '</a>';
if ( empty( $feed_image ) )
$link .= ')';
}
if ( $optioncount )
$link .= ' ('. $posts . ')';
$return .= $link;
$return .= ( 'list' == $style ) ? '</li>' : ', ';
}
$return = rtrim($return, ', ');
if ( !$echo )
return $return;
echo $return;
}
@patrick-wc
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment