0

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>
1

1 Answer 1

2

As 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.
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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