Ferodynamics Network

popular: profile privacy, mobile privacy

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

Wordpress Chat
one comment
page 1308
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Silence Is Golden
3 comments
page 213
Questions About Habari For Wordpress Users
6 comments
page 424
Theming Habari Vs Wordpress
13 comments
page 440
My Experience Of Flexx
4 comments
page 1026
Plugin Update Fun With Photo Data 2
one comment
page 815
Post Image The Easy Peasy Way
26 comments
page 1065
Categories Vs Tags Either Neither Or Both
12 comments
page 7
Gaining Benefits From Plugins
8 comments
page 1167
Fun With Theme Widgets
24 comments
page 867
Beware Wp Cache
8 comments
page 1310
Six Million Ways To Die Choose One
14 comments
page 1128
Post Image The Easy Peasy Way
26 comments
page 1065
Post Image The Easy Peasy Way
26 comments
page 1065
Wordpress Chat
one comment
page 1308
Post Image The Easy Peasy Way
26 comments
page 1065
Wordpress Chat
one comment
page 1308
Beware Wp Cache
8 comments
page 1310
Wp Polls Reviewed
one comment
page 58
Fun With Photo Data
12 comments
page 330
Html 5 Gallery
6 comments
page 1305
Fun With Sidebar Tabs Styling
2 comments
page 336
Using Your Own Url Shortener
4 comments
page 1190
Html 5 Gallery
6 comments
page 1305
My Experience Of Flexx
4 comments
page 1026
Fun With Sidebar Tabs
193 comments
page 57
Html 5 Gallery
6 comments
page 1305
Fun With Plugins
27 comments
page 14
Wordpress 25 Exif Fields
12 comments
page 230
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Beware Wp Cache
8 comments
page 1310
Quick N Dirty Replacement Text
no comment
page 122
Theming Habari Vs Wordpress
13 comments
page 440
Fun With Sidebar Tabs Styling
2 comments
page 336
Beware Wp Cache
8 comments
page 1310
  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?