作者按字母顺序划分

时间:2015-01-28 作者:selfagency

我想在我的网站上创建一个页面,列出该网站的所有贡献者及其个人简历、URL等信息。。

我发现this code 非常适合我的需要。然而,我想做的是让这段代码按姓氏的字母顺序(如果没有全名,则按用户名)将贡献者分解,并为每个字母添加标题和锚定标记。即:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

A

Bill Abrams
Tom Anderson

B

John Biskup
Steve Botwin

C

etc.
关于如何修改上面的链接代码以实现这一点,您有什么想法吗?

1 个回复
最合适的回答,由SO网友:selfagency 整理而成

这就是我的结局。完全做到了,减去按姓氏排序。我很乐意听到更多关于这方面的想法。

$display_admins = false;
$order_by = \'display_name\'; // \'nicename\', \'email\', \'url\', \'registered\', \'display_name\', or \'post_count\'
$role = \'\'; // \'subscriber\', \'contributor\', \'editor\', \'author\' - leave blank for \'all\'
$avatar_size = 32;
$hide_empty = true; // hides authors with zero posts
$last = \'\';

if(!empty($display_admins)) {
    $blogusers = get_users(\'orderby=\'.$order_by.\'&role=\'.$role);
} else {
    $admins = get_users(\'role=administrator\');
    $exclude = array();
    foreach($admins as $ad) {
        $exclude[] = $ad->ID;
    }
    $exclude = implode(\',\', $exclude);
    $blogusers = get_users(\'exclude=\'.$exclude.\'&orderby=\'.$order_by.\'&role=\'.$role);
}
$authors = array();
foreach ($blogusers as $bloguser) {
    $user = get_userdata($bloguser->ID);
    if(!empty($hide_empty)) {
        $numposts = count_user_posts($user->ID);
        if($numposts < 1) continue;
    }
    $authors[] = (array) $user;
}

echo \'<ul class="contributors">\';
foreach($authors as $author) {
    $current = strtolower($author[\'data\']->display_name[0]);
    $display_name = $author[\'data\']->display_name;
    $avatar = get_avatar($author[\'ID\'], $avatar_size);
    $author_profile_url = get_author_posts_url($author[\'ID\']);

    if ($last != $current) {
        echo \'<a class"alphabit" name="\' . strtoupper($current) . \'">\' . strtoupper($current) . \'</a>\';
            $last = $current;
    }
    echo \'<li><a href="\', $author_profile_url, \'">\', $avatar , \'</a><a href="\', $author_profile_url, \'" class="contributor-link">\', $display_name, \'</a></li>\';
}
echo \'</ul>\';

结束