What I would like to do is have a list of users who have contributed at least one post.
I need to show the following:
[User photo] | [User Name] | [User post count]
e.g.
[photo] Joe Bloggs(8)
I made a start and went down this route:
<?php
$blogusers = get_users( 'orderby=post_count' );
foreach ( $blogusers as $user ) {
echo '<li>' . esc_html( $user->display_name ) . '</li>';
}
?>
However, this seems to just return all users registered to the blog rather than those who have contributed so I’m certainly not doing it correctly.
I am new to wordpress and PHP so all help would be appreciated.
3 s
You need to set the who
parameter in get_users
<?php
$blogusers = get_users( 'orderby=post_count&who=authors' );
foreach ( $blogusers as $user ) {
echo '<li>' . esc_html( $user->display_name ) . '</li>';
}
?>
EDIT
Seems I was to fast answering. The code in your question and in my answer is the start to what you want to achieve.
I don’t have time to code now, off to watch rugby, but here is the complete code used in the twenty fourteen to display authors and their post count. Hope this helps
function twentyfourteen_list_authors() {
$contributor_ids = get_users( array(
'fields' => 'ID',
'orderby' => 'post_count',
'order' => 'DESC',
'who' => 'authors',
) );
foreach ( $contributor_ids as $contributor_id ) :
$post_count = count_user_posts( $contributor_id );
// Move on if user has not published a post (yet).
if ( ! $post_count ) {
continue;
}
?>
<div class="contributor">
<div class="contributor-info">
<div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
<div class="contributor-summary">
<h2 class="contributor-name"><?php echo get_the_author_meta( 'display_name', $contributor_id ); ?></h2>
<p class="contributor-bio">
<?php echo get_the_author_meta( 'description', $contributor_id ); ?>
</p>
<a class="button contributor-posts-link" href="https://wordpress.stackexchange.com/questions/158095/<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
<?php printf( _n( '%d Article', '%d Articles', $post_count, 'twentyfourteen' ), $post_count ); ?>
</a>
</div><!-- .contributor-summary -->
</div><!-- .contributor-info -->
</div><!-- .contributor -->
<?php
endforeach;
}
Simply call it in your template files as
twentyfourteen_list_authors();