Question

I'm trying to do something I assumed would be easy. I assumed wrong. What I want to do is list all the authors of a site on the sidebar, with a link to their respective author page, number of posts written, and gravatar. wp_list_authors doesn't give me an option to include the gravatar at all. Here is what I have

        $blogusers = get_users_of_blog();
        if ($blogusers) {
          foreach ($blogusers as $bloguser) {
            $user = get_userdata($bloguser->user_id);
        $post_count = count_user_posts($user->ID);
            if ($post_count) {
        echo '<li>';
        echo '<a href="'.get_bloginfo('url').'/author/' . $user->user_nicename . '">'.get_avatar($user->user_email, '36').'</a>';
        echo '<a href="'.get_bloginfo('url').'/author/' . $user->user_nicename . '">'.$user->display_name.' ('.$post_count.')</a><li>';
            }
          }
        }

Which works, albeit with two problems:

  1. I can't find a way to have them sort by number of posts
  2. the get_users_of_blog is a depreciated function. I really don't want to use it.

I was able to create an explode with wp_list_authors, but didn't have a clue how to extract the data out of it to display in this manner. Help?

Was it helpful?

Solution

To answer your problems:

Sorting by post count

You can collect all the information you need in to an array and sort that array by number of posts , like this:

    //function to sort array by filed
    function authors_orderBy($data, $field){
       $code = "if (\$a['$field'] == \$b['$field']) {return 0;} return (\$a['$field'] < \$b['$field']) ? 1 : -1;";
       usort($data, create_function('$a,$b', $code));
       return $data;
    }

then change your code a bit like so:

        $blogusers = get_users_of_blog();
        if ($blogusers) {
            $au = array();
            foreach ($blogusers as $bloguser) {
               $user = get_userdata($bloguser->user_id);
               $post_count = count_user_posts($user->ID);
               $au[] = array('user_id' => $user->ID , 'nicename' => $user->user_nicename, 'display_name' => $user->display_name, 'email' => $user->user_email ,'post_count' => $post_count);
            }

            //Sort array
            $au = authors_orderBy($au, 'post_count');

           //then loop through the authors
           foreach ($au as $aut){
             if ($aut['post_count'] > 0) {
                echo '<li>';
                echo '<a href="'.get_bloginfo('url').'/author/' . $aut['nicename'] . '">'.get_avatar($aut['email'], '36').'</a>';
                echo '<a href="'.get_bloginfo('url').'/author/' . $aut['nicename'] . '">'.$aut['display_name'] .' ('.$aut['post_count'].')</a><li>';
             }
           }
        }

get_users_of_blog is a depreciated

It's weird because looking at the codex Yes this function is depreciated and you should use get_users() which should be shipped with Version 3.1

     /**
     * Retrieve list of users matching criteria.
     *
     * @since 3.1.0
     * @uses $wpdb
     * @uses WP_User_Query See for default arguments and information.
     *
     * @param array $args
     * @return array List of users.
     */
    function get_users( $args ) {

            $args = wp_parse_args( $args );
            $args['count_total'] = false;

            $user_search = new WP_User_Query($args);

            return (array) $user_search->get_results();
    }

but if you look at wp_list_authors it uses get_users_of_blog() be itself.

Hope this helps.

OTHER TIPS

get_users_of_blog() may be deprecated per documentation, but it is actually what wp_list_authors() uses internally ( source ) and there seems to be no viable alternative available at moment.

I see no easy way to customize wp_list_authors() the way you want. Can only suggest to reuse code for it in you own function, modification to add gravatar will be minimal.

code widget for sidebar:

add_action( 'widgets_init', 'latest_members_widgets' );

function latest_members_widgets() {
register_widget( 'Latest_Members' );
}
class Latest_Members extends WP_Widget {
function Latest_Members() {
    $widget_ops = array( 'classname' => 'latest_members', 'description' =>   'Visualizza gli avatar degli utenti iscritti.' );
    $this->WP_Widget( 'latest-members-widget', 'Iscritti Recenti', $widget_ops);
}


 function widget( $args, $instance ) {
    extract( $args );

    $title = apply_filters('widget_title', $instance['title'] );
    $avatars = $instance['avatars']; ?>

    <?php echo $before_widget; ?>

    <?php if ( $title ) echo $before_title . $title . $after_title; ?>

    <div class="widget_latest_members">
    <ul>
        <?php global $wpdb;
        $szSort = "user_registered";
        $aUsersID = $wpdb->get_col( $wpdb->prepare(
        "SELECT $wpdb->users.ID FROM $wpdb->users ORDER BY ID DESC LIMIT $avatars", $szSort ));
        foreach ( $aUsersID as $iUserID ) :
        $user = get_userdata( $iUserID );
        echo '<a href="'.get_bloginfo(url).'/?author='.$iUserID.'" rel="profilo" title="Vai al Profilo">'.get_avatar($iUserID, 35,$default=$author->ID).'</a>';
        endforeach; ?>
        <br />
  <div class="totale"><?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); echo $users." utenti registrati";?></div>
</ul>
    </div>

    <?php echo $after_widget; }

function update( $new_instance, $old_instance ) {
    $instance = $old_instance;

    $instance['title'] = strip_tags( $new_instance['title'] );
    $instance['avatars'] = strip_tags( $new_instance['avatars'] );

    return $instance;
}

function form( $instance ) {

    /* Set up some default widget settings. */
    $defaults = array( 'title' => 'Latest Members', 'avatars' => 20);
    $instance = wp_parse_args( (array) $instance, $defaults ); ?>

    <p>
        <label for="<?php echo $this->get_field_id( 'title' ); ?>">Titolo:</label>
        <br/><input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( 'avatars' ); ?>">Numero Avatar da visualizzare:</label>
        <br/><input type="text" id="<?php echo $this->get_field_id( 'avatars' ); ?>" name="<?php echo $this->get_field_name( 'avatars' ); ?>" value="<?php echo $instance['avatars']; ?>" />
    </p>

<?php
}
    }

    ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top