Add gravatar to author list

I am using this code for listing out all authors on the site in my sidebar. It works, except I also need to pull in their Gravatar image. It’s working in a loop on the homepage with this

<?php echo get_avatar( get_the_author_email(), '80' ); ?>

but is there a way I can add it to this list as well?
Also.. I can’t figure out a way to exclude the “Admin” account using this code, is that possible?

Read More

Thank you!

<?php
$order = 'user_nicename';
$user_ids = $wpdb->get_col("SELECT ID FROM $wpdb->users ORDER BY $order"); // query users
foreach($user_ids as $user_id) : 
$user = get_userdata($user_id);
?>
<li><?php echo '<a href="' . $user->user_url . '">' . $user->display_name . '</a>'; ?><br /></li>
<?php
endforeach; 
?>

Related posts

Leave a Reply

3 comments

  1. Basic setup

    <?php
    
        $args = array( 'orderby' => 'nicename' );
        $users = get_users( $args ); 
    
        foreach ( $users as $user ) {
            $avatar = get_avatar( $user->ID, '80' );
            echo '<li><a href="' .
                    $user->user_url .
                '">' .
                    $avatar . '<br />' .
                    $user->display_name .
                '</a></li>';
        }
    
     ?>
    

    Excluding the Admin User

    Either check in the foreach:

    foreach ( $users as $user ) {
        if( ! in_array( 'administrator', $user->roles ) ) {
            // echo user list
        }
    }
    

    or if all other users are subscribers, include the role parameter as an argument for the user query:

    $args = array(
        'orderby' => 'nicename',
        'role' => 'subscriber'
    );
    

    or, if you have but one (or few static) admin user, exclude him/her from the query by id:

    $args = array(
        'orderby' => 'nicename',
        'exclude' => array( 1, 23 ) //adjust
    );
    
  2. Here’s a simple example showing default avatars and lists all users with the role of author.

    foreach ( get_users( array( 'role' => 'author'  ) ) as $user )
    {
        echo get_avatar(
            $user->ID,
            '96',
            get_stylesheet_directory_uri().'/default-avatar.png',
            $user->nice_name
        );
    }