Hi,
I have a custom function written that lists authors and displays them in a list with their user photos, bio and other stuff. It displays all authors at all user levels/roles though and I need to filter it. Ideally I'd be able to pass a role parameter like you might in wp_list_authors. That way I could reuse this in several places. This is my function and a sample of the parameter I'd like to add:
<?php custom_list_authors(
'role' => administrator,contributor; ); ?>
SO they question is: Can someone write into this custom function the parameter that I can reuse when needed?
Here is the code for the custom function:
function custom_list_authors($args = '') {
global $wpdb;
$defaults = array(
'orderby' => 'name', 'order' => 'ASC', 'number' => '10',
'optioncount' => false, 'exclude_admin' => true,
'show_fullname' => false, 'hide_empty' => true,
'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
'style' => 'list', 'html' => true
);
$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 );
$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 ) {
$return .= '<li>';
}
$link = '<p><a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a></p>' . "\n";
$link .= "<div class=\"image\">\n";
$link .= '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '">' . get_avatar( $author->ID, 23 ) . "</a>\n";
$link .= "</div>\n";
$link .= "<div class=\"box-inf\">\n";
$link .= "<div class=\"holder\">\n";
$link .= "<div class=\"frame\">\n";
$link .= "<div class=\"inf\">\n";
$link .= '<p><a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '">' . $name . "</a></p>\n";
$link .= "<div class=\"image\">\n";
$link .= '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '">' . get_avatar( $author->ID, 23 ) . "</a>\n";
$link .= "</div>\n";
$link .= '<span class="txt">' . get_the_author_meta('description', $author->ID) . "</span>\n";
$link .= "</div>\n";
$link .= "<div class=\"box-btn\">\n";
$link .= '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '"><span>Read Now</span></a>' . "\n";
$link .= "</div>\n";
$link .= "</div>\n";
$link .= "</div>\n";
$link .= "</div>\n";
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>\n" : ', ';
}
$return = rtrim($return, ', ');
if ( !$echo )
return $return;
echo $return;
}
Arnav Joy answers:
try this
<?php
function custom_list_authors($args = '',$role='') {
global $wpdb;
$defaults = array(
'orderby' => 'name', 'order' => 'ASC', 'number' => '10',
'optioncount' => false, 'exclude_admin' => true,
'show_fullname' => false, 'hide_empty' => true,
'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
'style' => 'list', 'html' => true
);
$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';
$query_args['role'] = $role;
$authors = get_users( $query_args );
$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 ) {
$return .= '<li>';
}
$link = '<p><a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a></p>' . "\n";
$link .= "<div class=\"image\">\n";
$link .= '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '">' . get_avatar( $author->ID, 23 ) . "</a>\n";
$link .= "</div>\n";
$link .= "<div class=\"box-inf\">\n";
$link .= "<div class=\"holder\">\n";
$link .= "<div class=\"frame\">\n";
$link .= "<div class=\"inf\">\n";
$link .= '<p><a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '">' . $name . "</a></p>\n";
$link .= "<div class=\"image\">\n";
$link .= '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '">' . get_avatar( $author->ID, 23 ) . "</a>\n";
$link .= "</div>\n";
$link .= '<span class="txt">' . get_the_author_meta('description', $author->ID) . "</span>\n";
$link .= "</div>\n";
$link .= "<div class=\"box-btn\">\n";
$link .= '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '"><span>Read Now</span></a>' . "\n";
$link .= "</div>\n";
$link .= "</div>\n";
$link .= "</div>\n";
$link .= "</div>\n";
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>\n" : ', ';
}
$return = rtrim($return, ', ');
if ( !$echo )
return $return;
echo $return;
}
?>
<?php custom_list_authors($args ,'administrator'); ?>
then use it as
dkraljic comments:
Hey Thanks! worked great!
I'll have another similar question shortly I bet you could help out with.
John Cotton answers:
get_users supports a role argument - you can check it out here:
http://codex.wordpress.org/Function_Reference/get_users
Sabby Sam answers:
Hi,
Can you try to use this plugins :
http://wordpress.org/extend/plugins/author-profiles/
or use this one
http://webinsolution.com/plugins/wordpress-author-profile-avatars
If you don't understand than drop an email me at [email protected], this above plugin is developed by me.
Hope the above plugin will help you.