WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display list of authors who have already posted (5 posts)

  1. Jeremy Herve
    Member
    Posted 4 years ago #

    Hi!

    I am having a small issue building an authors list page, thanks to a page template. I can display all users, but I don't know how to limit that list to users who posted already.

    Here is my code so far:

    <?php /*
    Template Name: Authors
    */ ?>
    
    <?php get_header(); ?>
    
    <div id="content">
    
    		<div class="breadcrumbs">
    			<?php the_breadcrumb(); ?>
    		</div>
    
    <?php the_post(); ?>
    
    	<div id="post-<?php the_ID(); ?>" <?php post_class('post'); ?>>
    		<h1 class="entry-title"><?php the_title(); ?></h1>
    		<div class="entry-content">
    			<?php the_content(); ?>
    		</div><!-- .entry-content -->
    	</div><!-- #post-<?php the_ID(); ?> -->
    
    <?php
    
    // Get the authors from the database ordered by user nicename
    	global $wpdb;
    	$query = "SELECT ID, user_nicename from $wpdb->users ORDER BY user_nicename";
    	$author_ids = $wpdb->get_results($query);
    
    // Loop through each author
    	foreach($author_ids as $author) :
    
    	// Get user data
    		$curauth = get_userdata($author->ID);
    
    	// If user level is above 0 or login name is "admin", display profile
    		if($curauth->user_level > 0 || $curauth->user_login == 'admin') :
    
    		// Get link to author page
    			$user_link = get_author_posts_url($curauth->ID);
    
    		// Set default avatar (values = default, wavatar, identicon, monsterid)
    			$avatar = 'default';
    ?>
    
    <div class="authors-desc">
    
    	<a href="<?php echo $user_link; ?>" title="<?php echo $curauth->display_name; ?>">
    		<?php echo get_avatar($curauth->user_email, '96', $avatar); ?>
    	</a>
    
    	<h2 class="post-title">
    		<a href="<?php echo $user_link; ?>" title="<?php echo $curauth->display_name; ?>"><?php echo $curauth->display_name; ?></a>
    	</h2>
    
    	<p>
    		<?php echo $curauth->description; ?>
    	</p>
    
    	<p>
    	<a href="<?php echo $user_link; ?>" title="<?php echo $curauth->display_name; ?>">View all his posts</a>
    	</p>
    
    	<div class="clear"></div>
    
    </div>
    
    		<?php endif; ?>
    
    	<?php endforeach; ?>
    
    </div><!-- end content -->	
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Any idea?

    Thanks!

  2. MichaelH
    Member
    Posted 4 years ago #

    $userposts = get_posts('showposts=-1&author='.$author->ID);
    $count=count($userposts);  //number of posts
    if ($count > 0) {
    //do display
    }
  3. Jeremy Herve
    Member
    Posted 4 years ago #

    Thank you Michael!

    However I am not sure I know where to insert that bit... :s Can you help?

  4. MichaelH
    Member
    Posted 4 years ago #

    after

    foreach($author_ids as $author) :

    put

    $userposts = get_posts('showposts=-1&author='.$author->ID);
    $count=count($userposts);  //number of posts
    if ($count > 0) {

    and change:

    <?php endforeach; ?>

    to

    <?php }
      endforeach; ?>
  5. Jeremy Herve
    Member
    Posted 4 years ago #

    Works great, thank you very much!

Topic Closed

This topic has been closed to new replies.

About this Topic