WordPress Top Authors List Without a Plugin
WordPress engine allows to create sites with big number of users of different levels. And authors, which have corresponding right, can fill the site content. On such a multi-user sites will be useful to have a list with top authors.
In this post I will show you one of the ways how to display list of the top authors without using a plugin.
You can use the following small function:
function top_authors($number = 10) { $uc = array(); $blogusers = get_users(); if ($blogusers) { echo '<ul>'; foreach ($blogusers as $bloguser) { $post_count = count_user_posts($bloguser->ID); $uc[$bloguser->ID] = $post_count; } arsort($uc); $i = 0; foreach ($uc as $key => $value) { $i++; if ($i <= $number) { $user = get_userdata($key); $author_posts_url = get_author_posts_url($key); $post_count = $value; if ($post_count > 0) { echo '<li><a href="' . $author_posts_url .'">' . $user->display_name . '</a> (' . $post_count . ')</li>'; } } } echo '</ul>'; } }
Paste this function in the functions.php file of your theme and then put the following code in a place of your theme, where you want to see the top authors list:
<?php if (function_exists('top_authors_list')) top_authors_list(10); ?>
10 here is the number of authors you want to show in the list.
That’s all.
As a result you will see the list of top authors with their names and number of published posts. Author’s name is linked to his archive page where is a list of all his posts.
List of top authors is sorted by number of posts.
Why the unnecessary plugin hate? They’re almost always better than filling up your functions.php…
http://kovshenin.com/2012/01/plugins-vs-without-a-plugin-3745/
I prefer don’t use plugins if possible do something without them.
How would I use Transients API to cache this?
Don’t know. I’m not familiar with Transients API.
That’s no problem, I will mess around with it and see if I can get it working.
Also, when I enabled
define('WP_DEBUG', true);
, I got a few errors, including two from your code above:get_users_of_blog()
andget_usernumposts
were deprecated.I tried updating them with new WordPress functions but it gave another error. :(
Could you update the code to include the new functions, please. :)
I’ve updated it.
I did this a different way but got the same results, however I’m still having some trouble counting posts for custom post types.
Any ideas?
Sorry, I can’t help anything.
Not working this code in my site. I am not sure why happen….