logo
  • Home
  • About
  • Plugins

Subscribe to Articles

Authors in Comments

Author: andrew Category: Uncategorized Tags: Snippets, Themery

Thursday
Mar 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.

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){
?>
 
	<?php the_author(); ?>
	<?php the_author_posts_link (); ?>
 
<?php
}
 
?>

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


Share:
image image image image image

Comments

Rebecca

March 30th, 2008 at 10:24 am

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?

Andrew (http://www.wp-fun.co.uk)

March 30th, 2008 at 1:20 pm

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?


Weren’t you doing a theme?
Premium Theme Reviews

Recommended

wordpress unlimited

Archives

  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007

Tags

2.7 2.8 Admin Advertising Air Blogging blogs Cache Cliche Coding Comments Content Types CSS Curry Death development Disqus Features Habari image Interview Licensing Modes Monday Poll New Features Organisation Personal Platform Plugin Plugin-Practices plugins Plugin update Poll premium Readers Reviews Search Simplification Snippets Spam Themery Themes tools User Interface Widgets WLTC

Copyright 2009 Fun with WordPress - All Rights reserved.

Wordpress theme by: WPUnlimited