0

I'd like to create a page on my site listing all of the site's contributors with info like their bio, URL, etc..

I found this code which is perfectly suited for my needs. However, what I'd like to do is have this code break contributors up alphabetically by last name (or by username if a full name is unavailable) with a header and anchor tag for each letter. Ie.:

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.

Any thoughts on how to modify the linked code above to achieve that?

  • If you want solid answers, I'd recommend copying the relevant code into your question. This would save those willing to answer the time spent hunting it down. – Privateer Jan 28 '15 at 1:22
  • 1
    This questions requires work on your part, that example you linked is bad code, use WP_User_Query then parse the results to create alphabetical lists with something like stackoverflow.com/questions/7812097/… – Wyck Jan 28 '15 at 1:56
  • @Wyck, you're my hero. Here's what I ended up with: gist.github.com/74ea28d039b5165ae6f3 – selfagency Jan 28 '15 at 3:52
  • You should add that as an answer below. – Wyck Jan 28 '15 at 4:18
1

This is what I ended up with. Totally does the trick, minus the sorting by last name. Would love to hear any additional thoughts on that.

$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>';
  • Just remember to accept your own answer when the 2 day restriction is lifted. :-) – Pieter Goosen Jan 29 '15 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.