WordPress自带的“作者函数”中没有能完成“列表文章最多的数个作者”的功能,如果你想在侧边栏中放一个“发表文章前三名”这样一个列表,那就要自己进行编程了。
以下就是我根据WordPress自带的wp_list_authors()函数进行修改得到的“列表文章最多的作者”的函数
function b_list_top_author($args = ''){
global $wpdb;
$defaults = array(
'optioncount' => false, 'exclude_admin' => true,
'show_fullname' => false, 'hide_empty' => true,
'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
'num' => '5'
);
$r = wp_parse_args( $args, $defaults );
extract($r, EXTR_SKIP);
$return = '';
$num = (int) $num;
if ($num < 1)
$num = 5;
$authors = $wpdb->get_results("SELECT DISTINCT u.ID, u.user_nicename, COUNT( p.ID ) AS count from $wpdb->users AS u, $wpdb->posts AS p " . ($exclude_admin ? "WHERE u.user_login <> 'admin' " : 'WHERE ') . "AND p.post_author = u.ID AND p.post_type = 'post' AND ( p.post_status = 'publish' OR p.post_status = 'private') GROUP BY p.post_author ORDER BY count DESC, display_name ASC LIMIT $num");
$author_count = array();
foreach ((array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row) {
$author_count[$row->post_author] = $row->count;
}
foreach ( (array) $authors as $author ) {
$author = get_userdata( $author->ID );
$posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
$name = $author->display_name;
if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
$name = "$author->first_name $author->last_name";
if ( !($posts == 0 && $hide_empty) )
$return .= '<li>';
if ( $posts == 0 ) {
if ( !$hide_empty )
$link = $name;
} else {
$link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . sprintf(__("Posts by %s"), attribute_escape($author->display_name)) . '">' . $name . '</a>';
if ( (! empty($feed_image)) || (! empty($feed)) ) {
$link .= ' ';
if (empty($feed_image))
$link .= '(';
$link .= '<a href="' . get_author_rss_link(0, $author->ID, $author->user_nicename) . '"';
if ( !empty($feed) ) {
$title = ' title="' . $feed . '"';
$alt = ' alt="' . $feed . '"';
$name = $feed;
$link .= $title;
}
$link .= '>';
if ( !empty($feed_image) )
$link .= "<img src=\"$feed_image\" style=\"border: none;\"$alt$title" . ' />';
else
$link .= $name;
$link .= '</a>';
if ( empty($feed_image) )
$link .= ')';
}
if ( $optioncount )
$link .= ' ('. $posts . ')';
}
if ( !($posts == 0 && $hide_empty) )
$return .= $link . '</li>';
}
if ( !$echo )
return $return;
echo $return;
}
使用方法,将上面的函数复制粘贴到主题的functions.php文件(没有就建一个吧)中,保存。
在需要列表的地方,插入进行调用。
该函数说白了,就是比wp_list_authors()多了一个num参数,你可以通过赋予num值来控制列表作者的数量。
参数:
optioncount:是否显示文章数。这个参数不可用。
exclude_admin:是否排除admin管理员帐号,默认值是true,排除该帐号的,你可以通过exclude_admin=false加入该帐号的。
show_fullname:是否显示全名。默认值是false,不显示全名,显示display_name。可选true。
hide_empty:是否隐藏没有文章的帐号。默认值是true,隐藏。可选false。
feed:是否显示该作者文章的供稿种子。默认为空白,也即是不显示。
feed_image:是否显示供稿种子的图标。默认为空白,也即是不显示。
feed_type:此参数貌似无用。
echo:是否打印HTML。默认为true,当然是打印的。可选false。
num:列表作者数量。默认为5个作者的。值为大于1的整数值。
例如你想在侧边栏放一个“文章最多的三位作者,包含admin帐号,显示作者的供稿种子链接,打印HTML,不显示全名的列表,这样调用:
<ul>
<?php b_list_top_author('exclude_admin=false&feed=1&echo=true&show_fullname&num=5')?>
</ul>