888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

March 27, 2008

Earlier today Miriam at WordPress Garage posted a quick challenge: how to display a list of authors who had posted in a particular category. I had an idea at the time but I wasn’t in a position to test the idea, so now I am home I have written a quick function to do just that.

The function

These two functions are best placed in your functions.php file or can be added loose to the category template file, either would be fine.

[php]
function get_authors_by_cat($cat_id, $sort = true){

$author_id_array = array();
$author_details_array = array();

$cat_posts = get_posts('category='.(int) $cat_id);

foreach ($cat_posts as $cat_post){
if (!in_array($cat_post->post_author , $author_id_array)){
$author_id_array[] = $cat_post->post_author;
$author_details_array[] = get_userdata($cat_post->post_author);
}
}

if ($sort === true ){
usort($author_details_array , 'author_by_cat_sort');
}

return $author_details_array;
}

function author_by_cat_sort($a, $b){
$al = strtolower($a->display_name);
$bl = strtolower($b->display_name);
if ($al == $bl) {
return 0;
}
return ($al > $bl) ? +1 : -1;
}

There are two functions here, the first retrieves all the posts from the category and loops through them to make sure. The second function is a callback for the usort function. This allows the list to be sorted by the display name of the authors.

This could be done more efficiently by querying the database directly but I wanted to avoid that option as a lot of people are not comfortable doing that themselves.

Using the Function

To use the function in your theme you need to include the following in your where you want the list to appear (replacing the number 18 with the ID of the category that you want to list authors for.

[php]

$authordata_array = get_authors_by_cat(18, $sort = true);
foreach ($authordata_array as $authordata){
?>


}

?>

Notice that in the foreach loop you should be able to use the normal author based template tags. You can find a list at http://codex.wordpress.org/Template_Tags

Using Wordpress As A Php Framework
Using wordpress as a framework?
2 comments
page 335
Using Your Own Url Shortener
Rewriterule shorten url?
4 comments
page 1190
Post Image The Easy Peasy Way
Finding attached image with post in wordpress?
26 comments
page 1065
My Experience Of Flexx
How to edit flexx theme wordpress?
4 comments
page 1026
Wordpress 25 Exif Fields
Wordpress exif plugin?
12 comments
page 230
Post Image The Easy Peasy Way
List image attachments wordpress?
26 comments
page 1065
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Wp image attached linkable?
26 comments
page 1065
Post Image The Easy Peasy Way
Wp get first image for post?
26 comments
page 1065
Updating Code Snippets Here
Cacheaboutblank?
no comment
page 1338
Html 5 Gallery
Html5 photo gallery?
6 comments
page 1305
Quick N Dirty Admin Login Screen
Simple login screen css?
no comment
page 128
Six Million Ways To Die Choose One
One million ways to die?
14 comments
page 1128
Updating Code Snippets Here
Interesting wordpress websites?
no comment
page 1338
Post Image The Easy Peasy Way
Attachment images in wordpress single posts?
26 comments
page 1065
Six Million Ways To Die Choose One
Is there an easy way to die?
14 comments
page 1128
Quick N Dirty Replacement Text
Addaction replacement?
no comment
page 122
Html 5 Gallery
Html5 photo gallery?
6 comments
page 1305
Are Child Themes The Best Option
Thesis child theme?
15 comments
page 1262
Charcoal Theme Available For Wordpress
Charcoal wordpress site admin?
2 comments
page 959
Post Image The Easy Peasy Way
Finding attached image with post in wordpress?
26 comments
page 1065
How To Add Sidebars To A Theme
Register sidebar in functionphp?
10 comments
page 1053
Why I Ditched Disqus
Disqus themes?
5 comments
page 1175
Post Image The Easy Peasy Way
How to use wp get attachment url?
26 comments
page 1065
Post Image The Easy Peasy Way
Finding attached image with post in wordpress?
26 comments
page 1065
What A Body
Whatabodybiz?
11 comments
page 1126
Html 5 Gallery
Html5 chat?
6 comments
page 1305
Adding Settings To Admin Pages
Admin add field wordpress?
3 comments
page 793
Wordpress Chat
Wp chat page?
3 comments
page 1308
Html 5 Gallery
Html5 and tabs?
6 comments
page 1305
Post Image The Easy Peasy Way
Finding attached image with post in wordpress?
26 comments
page 1065
Creating Custom Urls
Custom url rewrite wordpress?
6 comments
page 80
  1 query every 1558 seconds, updated 1 seconds ago.
Sunday, 1pm
 __
(__)
   `

 Andrew

Rebecca,

I had it working on WordPress 2.5 and 2.3. What errors did you get?

DId you copy and paste directly or did you change the fancy quotes to normal ones? i.e. the single quotes around the sections in red above?

Sunday, 10am
 __
(__)
   `

 Rebecca

Thanks for posting this code – I tried it out but got error warnings. I tried putting it in the functions.php and then tried putting it in the category template page with no luck. Did you have success in getting your code to work?