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 title showing space?
no comment on page 1371

Wordpress fun?
one comment on page 1376

Live blogging plugin?
4 comments on page 1258

Wordpress 3 admin speed up?
4 comments on page 1321

Framework photoshop?
3 comments on page 296

Fun wp plugins?
one comment on page 1376

Habari vs wordpress?
12 comments on page 440

Wp tags vs categories?
12 comments on page 7

Wordpress rss seo?
one comment on page 1361

Photo albums html5?
6 comments on page 1305

Wordpress chat?
no comment on page 1308

Wordpress exif data?
12 comments on page 230

Css sidear tab?
2 comments on page 336

Wordpress theme html5 blueprint?
6 comments on page 1305

Wordpress shortcode in plugin?
no comment on page 236

Html 50 photo album?
6 comments on page 1305

Get the post attachement?
24 comments on page 1065

Wordpress plugin development 30?
one comment on page 1373

Wordpress plugin development 30?
one comment on page 1373

Disqus formatting?
7 comments on page 1175

Html5 photoalbum?
6 comments on page 1305

Html5 photoalbum?
6 comments on page 1305

Wordpress fun?
one comment on page 1376

Fun wordpress plugins?
one comment on page 1376

Url shortener ideas?
4 comments on page 1190

Url shortener ideas?
4 comments on page 1190

Html 5 photo gallery?
6 comments on page 1305

Multiple post navigation?
no comment on page 1147

Html5 photo galleries?
6 comments on page 1305

Adding images to a wordpress 3 post?
24 comments on page 1065

Html5 photo gallery code?
6 comments on page 1305

Wordpress multiple blog master?
one comment on page 1376

Wordpress 3 tableprefix?
one comment on page 1376

Wordpress 3 tableprefix?
2 comments on page 1374

Using wordpress as a framework?
2 comments on page 335

Single post image size?
24 comments on page 1065

Get featured image src wordpress?
24 comments on page 1065

Disqus wordpress mu?
7 comments on page 1175

Image gallery html 5?
6 comments on page 1305

Wordpress theimage?
24 comments on page 1065

Wpgetattachmentimagesrc size?
24 comments on page 1065
  every 1732s, 1s ago, in 0.03s.
 __
(__)
   `
 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?

 __
(__)
   `
 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?


0s