我需要从我的贡献者页面中排除没有帖子的用户

时间:2012-10-27 作者:James Little

我的博客上有一个贡献者页面,我最近开始使用一些在线代码,这很好。

然而

我希望它能做两件额外的事情:

1) 仅列出在网站上至少有一篇批准帖子的成员2)列出之前5篇帖子的成员

这是我目前使用的代码,

<?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(); ?>
非常感谢您的帮助,

谢谢詹姆斯

1 个回复
SO网友:Mridul Aggarwal

// 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();
这段代码应该放在您的foreach循环中

结束