WordPress.org

Ready to get started?Download WordPress

Forums

How to show top authors with avatar and count (7 posts)

  1. turkishjapan
    Member
    Posted 4 years ago #

    For example http://wordtaps.com/ I want to show top authors with avatar and post count.

    Really i tried to make it but i ccouldn't. Anyone knows how can i do?

  2. MichaelH
    Member
    Posted 4 years ago #

    <?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 );
    
      }
    }
    ?>
  3. turkishjapan
    Member
    Posted 4 years ago #

    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.

  4. turkishjapan
    Member
    Posted 4 years ago #

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

  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    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 />
    	';
    }
    ?>
  6. turkishjapan
    Member
    Posted 4 years ago #

    @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.

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic