最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Limit posts per author role (excluding admin) in home page

matteradmin4PV0评论

I have multiple authors in my site and want to show just one, the latest post from each one, excluding the administrators. So, if for example:

  • admin1 has posted 4 posts
  • admin2 posted 3 posts
  • author1 posted 2 and
  • author2 posted 7,

it will show 4 from admin1, 3 from admin2 and 1 from each one of the others. Any hints?

<?php 
get_header(); 
//Displaying latest post per author on front page 
if(have_posts()) : 

    get_template_part('content', 'postlist'); 

else : 
    get_template_part('content', 'none'); 
endif;

get_footer();
?>

I have multiple authors in my site and want to show just one, the latest post from each one, excluding the administrators. So, if for example:

  • admin1 has posted 4 posts
  • admin2 posted 3 posts
  • author1 posted 2 and
  • author2 posted 7,

it will show 4 from admin1, 3 from admin2 and 1 from each one of the others. Any hints?

<?php 
get_header(); 
//Displaying latest post per author on front page 
if(have_posts()) : 

    get_template_part('content', 'postlist'); 

else : 
    get_template_part('content', 'none'); 
endif;

get_footer();
?>
Share Improve this question edited Jan 28, 2016 at 15:33 terminator 6151 gold badge6 silver badges30 bronze badges asked Jan 24, 2016 at 19:58 mausmaus 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

If I understand your problem then this should definitely work.

<?php 
get_header();

$users = get_users( array( 'who' => 'author' ) );//get all the users with author role in an array


foreach ( $users as $user ) {   //travers the array

    if($user->caps['administrator']==1)continue;     // skip the user if user also have administrative capabilities
$query = new WP_Query( array(
                            'posts_per_page'=>1, 
                            'author' => $user->ID
                            ) 
                    );
if($query->have_posts()):while($query->have_posts()):$query->the_post();

get_template_part('content', 'postlist'); 

endwhile;

else:
get_template_part('content', 'none'); 
endif;

} 

get_footer();
?>
Post a comment

comment list (0)

  1. No comments so far