我如何才能显示作者的最新帖子和标题?

时间:2019-10-17 作者:user176974

以下是我的代码:(klcom是我的主题名)

我只是通过以下方式显示作者:姓名和描述,我想在每个作者之后用平铺显示作者的最后3篇文章,如何编写代码?

谢谢

<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 个回复
SO网友:Anteraez

由于您已经拥有作者的ID,您可以创建一个新的WP\\U查询并获取作者的3篇最新帖子。

以下代码将为您完成此工作,

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

相关推荐

是否修改WP_LIST_TABLE分页“项目”文本?

我正在开发一个插件,在WP仪表板的页面上创建几个自定义表。我的问题是,是否有可能(在不修改核心文件的情况下)更改这些表上方分页中显示的文本?具体来说,我想更改“项/项”一词,以反映表中显示的对象类型。我已经查看了定义WP\\u List\\u Table类的核心文件,以查看该方法在何处定义该文本,但在扩展该类并传递分页参数时,似乎无法传递任何参数来修改它。。。下面是定义上图中显示的文本的代码的确切部分:$output = \'<span class=\"displaying-num\">\'