Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a contributors page on my blog that I have recently started and using some code online this is working pretty well.

HOWEVER

I would love it to do two extra things:

1) Only list members who have at least one approved post on the site 2) List the members previous 5 posts

Here is the code I am using currently,

<?php /*
Template Name: Authors
*/ ?>

<?php get_header(); ?>

<div id="content" class="clearfix">
<div id="main" class="col620 clearfix" role="main">

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->

<div class="entry-content post_content">
<?php the_content(); ?>
</div>


<?php endwhile; endif; ?>

<?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 = 'wavatar';
?>

<div id="container" style="padding:10px 10px;">
<div class="author single_postmeta">

<a href="<?php echo $user_link; ?>" title="<?php echo $curauth->display_name; ?>">
<?php echo get_avatar($curauth->user_email, '96', $avatar); ?>
</a>

<h3 class="post-title">
<a href="<?php echo $user_link; ?>" title="<?php echo $curauth->display_name; ?>"><?php         echo $curauth->display_name; ?></a><br>

<?php if(get_the_author_meta('jobtitle', $curauth->ID)): ?>
<?php the_author_meta('jobtitle', $curauth->ID); ?> at <?php the_author_meta('company', $curauth->ID); ?>
<?php endif; ?>

</h3>
<br>
<p><?php echo $curauth->description; ?> </p>
<br>

<ul>
<?php if(get_the_author_meta('twitter', $curauth->ID)): ?>
<li><a href='http://twitter.com/<?php the_author_meta('twitter', $curauth->ID); ?>'>Follow <?php the_author_meta('first_name', $curauth->ID); ?> on twitter  &rarr;</a></li>
<?php endif; ?>
<?php if(get_the_author_meta('facebook', $curauth->ID)): ?>
<li><a href='<?php the_author_meta('facebook', $curauth->ID); ?>'>Find <?php the_author_meta('first_name', $curauth->ID); ?> on Facebook  &rarr;</a></li>
<?php endif; ?>
<?php if(get_the_author_meta('linkedin', $curauth->ID)): ?>
<li><a href='<?php the_author_meta('linkedin', $curauth->ID); ?>'>Connect with <?php the_author_meta('first_name', $curauth->ID); ?> on Linkedin  &rarr;</a></li>
<?php endif; ?>
<?php if(get_the_author_meta('googleplus', $curauth->ID)): ?>
<li><a href='<?php the_author_meta('googleplus', $curauth->ID); ?>'>Add <?php the_author_meta('first_name', $curauth->ID); ?> to your circle on Google+ &rarr;</a></li>
<?php endif; ?>

</ul>

</div>
</div>

<?php endif; ?>
<?php endforeach; ?>
</article>

<?php get_footer(); ?>

Would appreciate any help you can give,

Thanks James

share|improve this question

1 Answer 1

// retrieve latest 5(or all) posts of this user
$query = new WP_Query(array(
    'author' => $author,
    'posts_per_page' => 5
));

// skip the user if the number of posts is 0
if($query->post_count < 1)
    continue;

// loop through available posts & display the title
while($query->have_posts()) :
    $query->the_post();
    the_title();
endwhile;
wp_reset_query();

This code should go inside the your foreach loop

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.