简体   繁体   English

获取作者在Wordpress中的先前帖子

[英]Get previous post by author in Wordpress

I want to display the previous post written by the current post's author at the bottom of a post. 我想在帖子底部显示由当前帖子作者撰写的先前帖子。

I know I can use the function get_previous_post() to get the previous post of the current post type, but it's not filterable by author. 我知道我可以使用函数get_previous_post()来获取当前帖子类型的前一个帖子,但是它不能被作者过滤。

I don't want just the most recent post by the author, but the one that came before the post that is being viewed. 我不仅想要作者的最新帖子,还希望查看该帖子之前的帖子。

This might work. 这可能有效。 $previous_post will be the last post by the author of the current post. $previous_post将是当前帖子作者的最后一个帖子。

<?php
$this_post = get_post();

$args = array(
    'author'        =>  $this_post->post_author,
    'post_type'     =>  $this_post->post_type,
    'orderby'       =>  'post_date',
    'order'         =>  'DESC',
    'date_query' => array(
        'before' => $this_post->post_date
    ),
);

$author_posts = get_posts( $args );
$previous_post = $author_posts[0];

I've used date_query which was added in WordPress 3.7: http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters 我使用了WordPress 3.7中添加的date_query: http ://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

This lets us take the current post's date and use that as our cut-off point. 这样我们就可以将当前帖子的日期用作截止日期。 After that it's relatively easy to grab what you need. 之后,获取您所需的东西相对容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM