Viewing 6 replies - 1 through 6 (of 6 total)
  • <?php
    //sort users descending by number of posts, display username and avatar
    $uc=array();
    $blogusers = get_users_of_blog();
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $post_count = get_usernumposts($bloguser->user_id);
        $uc[$bloguser->user_id]=$post_count;
      }
      arsort($uc);
      foreach ($uc as $key => $value) {
        $user = get_userdata($key);
        $author_posts_url = get_author_posts_url($key);
        $post_count = $value;
        echo '<p>User ID ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname . ' number of posts:' . $post_count . ' author posts url:' . $author_posts_url .'<p>';
        echo get_avatar( $user->ID, 46 );
    
      }
    }
    ?>
    Thread Starter turkishjapan

    (@turkishjapan)

    Thanks a lot but it has error.

    Notice: Undefined property: stdClass::$user_firstname in C:\xampplite\htdocs\wp\wp-content\themes\xxxxxxx\sidebar.php on line 31

    the line is:

    echo '<p>User ID ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname . ' number of posts:' . $post_count . ' author posts url:' . $author_posts_url .'<p>';

    Edit: i found the problem. from user_firstname and user_lastname doesn’t working. when i deleted it’s working but without names not ok.

    Thread Starter turkishjapan

    (@turkishjapan)

    <?php
    //sort users descending by number of posts, display username and avatar
    $uc=array();
    $blogusers = get_users_of_blog();
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $post_count = get_usernumposts($bloguser->user_id);
        $uc[$bloguser->user_id]=$post_count;
      }
      arsort($uc);
      foreach ($uc as $key => $value) {
        $user = get_userdata($key);
        $author_posts_url = get_author_posts_url($key);
        $post_count = $value;
     echo '<li>';
     echo '<a href="' . $author_posts_url .'">' . $user->user_nicename . '</a> (' . $post_count . ')';
     echo get_avatar( $user->ID, 4 );
     echo '</li>';
      }
    }
    ?>

    thats working.

    finally i need to show 5 or 10. thats coming too much almost all of members.

    Try this..

    <?php
    global $wpdb;
    $top_authors = $wpdb->get_results("
    	SELECT u.ID, count(post_author) as posts FROM {$wpdb->posts} as p
    	LEFT JOIN {$wpdb->users} as u ON p.post_author = u.ID
    	WHERE p.post_status = 'publish'
    	AND p.post_type = 'post'
    	GROUP by p.post_author
    	ORDER by posts DESC
    	LIMIT 0,10
    ");
    if( !empty( $top_authors ) )
    foreach( $top_authors as $key => $author ) {
    	echo '
    	<a href="' . get_author_posts_url( $author->ID ) . '">' . get_the_author_meta( 'nickname' , $author->ID ) . ' (' . $author->posts . ') posts </a>
    	<br />
    	';
    }
    ?>

    Thread Starter turkishjapan

    (@turkishjapan)

    @t31os_ Thank you but this is without avatars.

    <?php
    //sort users descending by number of posts, display username and avatar
    $uc=array();
    $blogusers = get_users_of_blog();
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $post_count = get_usernumposts($bloguser->user_id);
        $uc[$bloguser->user_id]=$post_count;
      }
      arsort($uc);
      foreach ($uc as $key => $value) {
        $user = get_userdata($key);
        $author_posts_url = get_author_posts_url($key);
        $post_count = $value;
     echo '<li>';
     echo '<a href="' . $author_posts_url .'">' . $user->user_nicename . '</a> (' . $post_count . ')';
     echo get_avatar( $user->ID, 4 );
     echo '</li>';
      }
    }
    ?>

    This is nice code but i need to only 10 authors. this is unlimited. thanks all.

    You could add avatars to what i provided before…

    <?php
    global $wpdb;
    $top_authors = $wpdb->get_results("
    	SELECT u.ID, count(post_author) as posts FROM {$wpdb->posts} as p
    	LEFT JOIN {$wpdb->users} as u ON p.post_author = u.ID
    	WHERE p.post_status = 'publish'
    	AND p.post_type = 'post'
    	GROUP by p.post_author
    	ORDER by posts DESC
    	LIMIT 0,10
    ");
    if( !empty( $top_authors ) ) {
    	echo '<ul>';
    	foreach( $top_authors as $key => $author ) {
    		echo '
    		<li>
    			<a href="' . get_author_posts_url( $author->ID ) . '">' . get_the_author_meta( 'user_nicename' , $author->ID ) . '</a>
    			(' . $author->posts . ') ' . get_avatar( $author->ID , 4 ) . '
    		</li>
    		';
    	}
    	echo '</ul>';
    }
    ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to show top authors with avatar and count’ is closed to new replies.