1

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?

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; 
?>

3 Answers 3

3

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
);
1
  • 1
    get_users should have a roles parameter that accepts multiple arguments as an array. Even better also an exclude_roles paramater. Oct 17, 2012 at 18:44
1

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
    );
}
0

use <?php echo get_avatar( $user->ID, '80' ); ?>, somewhere inside the foreach loop

If you're looking to exclude a particular user, you can use it's id($user->ID) or username($user->user_login) or if you want to exclude all the administrators, you can use if(current_user_can('promote_users')) continue; as the first line of foreach. Check out the reference for more info

http://codex.wordpress.org/Function_Reference/current_user_can

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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