显示4篇按时间顺序排列的帖子,从随机帖子开始

时间:2015-10-26 作者:Adam Lobo

我知道我可以通过如下操作显示4篇随机帖子:

get_posts(\'orderby=rand&numberposts=4\');
nbsp;

我想做的是从一个随机项目开始,然后按时间顺序显示接下来的3篇文章。

我的想法大致如下:

$posts = get_posts(\'orderby=rand&numberposts=1\'); 

foreach($posts as $post) { 
    the_title();

    //get next 3 chronological posts and loop
} 
我想我需要使用类似于“offset”的参数,但是要使用post id而不是position?

2 个回复
最合适的回答,由SO网友:birgire 整理而成

对于随机偏移,我们可以尝试:

$ppp    = 4;  
$total  = wp_count_posts()->publish;    

$offset = $total < $ppp ? 0 : rand( 0, $total - $ppp );

$posts = get_posts( [
    \'posts_per_page\' => $ppp,
    \'offset\'         => $offset
] );
示例:让我们$ppp 同于4并假设$total 是6。

然后是以下三种可能性:$offset, 即0、1和2:

Nr  Offset Selections
1   0      x
2   1      x x
3   2      x x x
4   3      x x x
5   4        x x
6   5          x
所以

$offset = $total < $ppp ? 0 : rand( 0, $total - $ppp );
将提供:

$offset = rand( 0, 6 - 4 );
或者只是

$offset = rand( 0, 2 );

SO网友:Pieter Goosen

下面是另一种使用date_query.

我们将获得一个随机帖子

然后我们将使用date_query 获取与该随机帖子相邻的其他3个帖子

以下是我们将使用的功能:(NOTE: 我对代码进行了注释,使其易于理解,代码需要PHP 5.4+)

function get_random_posts( $args = [], $direction = \'after\' )
{
    /**
     * Lets first get our random post, then work from there. We will be using the same 
     * exact arguments for all our queries we need to run. We do however need to modify
     * some a bit. We will save the default args to a another variable and then modify
     * the args to pass to the first query.
     *
     * We will let WP_Query handle the sanitation and validation from the
     * array of arguments.  
     */
    $random_args                   = $args;
    $random_args[\'orderby\']        = \'rand\';
    $random_args[\'posts_per_page\'] = 1;

    $random_post = get_posts( $random_args );

    /**
     * We will o get the adjacent posts from the random one. We will be 
     * using the default $args again
     *
     * We will need to sort out the amount of posts to get from the adjacent
     * post query first before  we go along. We need to deduct one from the amount
     * of posts to adjust for the random post
     */
    if ( isset( $args[\'posts_per_page\'] ) ) {
        $args[\'posts_per_page\'] = ( $args[\'posts_per_page\'] - 1 );
    } else {
        $args[\'posts_per_page\'] = ( get_option( \'posts_per_page\' ) - 1 );
    }

    // Create our date query to get the adjacent posts  
    $date_query = [
        [
            $direction  => $random_post[0]->post_date,
            \'inclusive\' => false
        ]
    ];
    $args[\'date_query\'] = $date_query;

    // Set the order parameter according to direction
    if ( $direction === \'after\' ) {
        $args[\'order\'] = \'ASC\';
    } else {
        $args[\'order\'] = \'DESC\';
    }

    $adjacent_query = get_posts( $args );   

    // Merge and return the posts
    return array_merge( $random_post, $adjacent_query );
}
如您所见,函数中的第一个参数是$args. 这将是一个参数数组,通常会传递给WP_Query. 第二个参数,$direction 将是相邻立柱的方向beforeafter 根据需要。

您可以使用以下功能:

$args = [
    \'posts_per_page\' => 4 // The amount of posts to get
    // Any other arguments you might need
];
$q = get_random_posts( $args );

foreach ( $q as $post ) {
    setup_postdata( $post );

    the_title();

}
wp_reset_postdata();

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请