Skip to content

Instantly share code, notes, and snippets.

@ellm
Forked from NateJacobs/gist:1321741
Last active October 25, 2022 05:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ellm/1325000 to your computer and use it in GitHub Desktop.
Save ellm/1325000 to your computer and use it in GitHub Desktop.
WordPress - List all authors of a blog grouped by first name with a single letter as a header character.
<?php
/**
* List Users Alphabetically
*
* Create a list of all authors of a WordPress blog.
* Names are grouped by first name with a single letter
* as a header character.
*
* Call the function with <?php ngtj_list_users_alphabetically(); ?>
* where you want the list to appear.
*
* @author Nate Jacobs
* @link https://gist.github.com/1321741
*/
function ngtj_list_users_alphabetically()
{
$users = get_users( 'orderby=user_login&role=author' );
$first_letter = '';
foreach( $users as $user )
{
$space = strpos( $user->user_login, ' ' );
$letter = substr( $user->user_login, 0, 1 );
$letter = strtoupper( $letter );
if ( $letter != $first_letter )
{
$first_letter = $letter;
echo "<h4 id='ft_contrib_alphaletter_$first_letter'>$first_letter</h4>";
}
echo '<a href="' . get_author_posts_url( $user->ID, $user->user_nicename ) . '" title="' . $user->display_name . '">' . $user->display_name . '</a>';
echo "<br>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment