The following is my code: (klcom is my theme name)
I just show the author by: name and description, I want to show the author's last 3 post with tile after each author, How can I code?
Thanks
<ul class="klcom-user-list user-cols-<?php echo $cols;?>">
<?php foreach ( $users as $user ){
$cover_photo = klcom_get_cover_url( $user->ID );
$group = klcom_get_user_group( $user->ID ); ?>
<li class="klcom-user-item">
<div class="klcom-user-cover"><?php echo klcom_lazyimg($cover_photo, $user->display_name);?></div>
<div class="klcom-user-avatar">
<a class="avatar-link" href="<?php echo get_author_posts_url( $user->ID );?>" target="_blank">
<?php echo get_avatar( $user->ID, 120, '', $user->display_name );?>
</a>
</div>
<div class="klcom-user-name">
<p>
<a href="<?php echo get_author_posts_url( $user->ID );?>" target="_blank"><?php echo $user->display_name;?></a>
<?php if($group){ ?><span class="wpcom-user-group"><?php echo $group->name;?></span><?php } ?>
<p class="author-description"> <? echo $user->description; ?></p>
</p>
</div>
</li>
<?php } ?>
</ul>
The following is my code: (klcom is my theme name)
I just show the author by: name and description, I want to show the author's last 3 post with tile after each author, How can I code?
Thanks
<ul class="klcom-user-list user-cols-<?php echo $cols;?>">
<?php foreach ( $users as $user ){
$cover_photo = klcom_get_cover_url( $user->ID );
$group = klcom_get_user_group( $user->ID ); ?>
<li class="klcom-user-item">
<div class="klcom-user-cover"><?php echo klcom_lazyimg($cover_photo, $user->display_name);?></div>
<div class="klcom-user-avatar">
<a class="avatar-link" href="<?php echo get_author_posts_url( $user->ID );?>" target="_blank">
<?php echo get_avatar( $user->ID, 120, '', $user->display_name );?>
</a>
</div>
<div class="klcom-user-name">
<p>
<a href="<?php echo get_author_posts_url( $user->ID );?>" target="_blank"><?php echo $user->display_name;?></a>
<?php if($group){ ?><span class="wpcom-user-group"><?php echo $group->name;?></span><?php } ?>
<p class="author-description"> <? echo $user->description; ?></p>
</p>
</div>
</li>
<?php } ?>
</ul>
Share
Improve this question
edited Oct 17, 2019 at 18:16
Sally CJ
40.3k2 gold badges29 silver badges50 bronze badges
asked Oct 17, 2019 at 17:53
user176974user176974
1
1
- Welcome to WordPress Development. I hope you find the answer(s) you are looking for. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. – Matthew Brown aka Lord Matt Commented Oct 18, 2019 at 2:12
1 Answer
Reset to default 2As you already have the author's ID, you can create a new WP_Query and get the 3 latest posts of the Author.
The following code will do the job for you,
$getPosts = new WP_Query(
array(
"author" => $user->ID, // This tells the query which author's post you want to get.
"posts_per_page" => 3, // This tells the query how many posts you want. In your case, 3.
"orderby" => date, // This tells the query that you want the posts according to their publish date.
"order" => "DESC", // This tells the query that you want to get the latest posts. If you change it to "ASC", then the query will get the oldest posts.
)
);
if($getPosts->have_posts()) {
while($getPosts->have_posts()) {
$getPosts->the_post();
echo "<h2 class=\"post-title\"><a href=\"" . get_the_permalink() . "\">" . get_the_title() . "</a></h2>"; // Echo the post title as a link to the post on the page.
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745071401a4609570.html
评论列表(0条)