WP_QUERY:按日期顺序显示10个帖子,前三个是随机的

时间:2018-02-21 作者:user2125467

我在网上找到了不同的代码片段,但找不到一个有效的解决方案。

基本上,我需要总共显示10个帖子,但前三个帖子需要是随机的。

这就是我到目前为止所做的(取自一个类似的问题)。

$args = array( 
    \'post_type\' => \'post\', 
    \'posts_per_page\'        => -1,
    \'orderby\'               => \'publish_date\',
    \'order\'                 => \'DESC\',
    \'_shuffle_and_pick\'     => 3 // <-- our custom argument
);
$loop = new \\WP_Query( $args );
在我的函数中使用以下函数。php

add_filter( \'the_posts\', function( $posts, \\WP_Query $query )
{
if( $pick = $query->get( \'_shuffle_and_pick\' ) )
{
    shuffle( $posts );
    $posts = array_slice( $posts, 0, (int) $pick );
}
return $posts;
}, 10, 2 );
但这只显示了三个随机帖子,仅此而已。

是否可以对此进行调整,使其总共显示10个,前3个是随机的,其余按日期顺序排列?

还是我需要一种新的方法?

2 个回复
SO网友:luukvhoudt

尝试以下操作:

// All the rendered HTML
$output = \'\';

// The posts to exclude from the random query.
$exclude = [];

// The remaining non-random posts
$remaining = new WP_Query([ 
   \'posts_per_page\' => 7,
]);

// Run the remaining query first because we have to know what should be excluded in the random query.
if {$remaining->have_posts()) {
    while ($remaining->have_posts()) {
        $remaining->the_post();

        // Ensure that all remainig posts are excluded from the random query.
        $exclude[] = get_the_ID();

        ob_start();

        // Render the output, consider using a function for consistency.
        the_title();
        the_content();

        // Gather and append the ouput.
        $output .= ob_get_clean();
    }
    wp_reset_query();
}

// Setup the random query with the excluded posts array.
$random = new WP_Query([
    \'posts_per_page\' => 3,
    \'exclude\' => $exclude,
    \'orderby\' => \'rand\',
]);

// Run the random query
if {$random->have_posts()) {
    while ($random->have_posts()) {
        $random->the_post();

        ob_start();

        // Again render the output, consider using a function for consistency.
        the_title();
        the_content();

        // Gather and prepend the ouput.
        $output = ob_get_clean() . $ouput;
    }
    wp_reset_query();
}

// Display the ouput
print $output;
您还可以改变这种情况,首先运行随机查询并存储它们的ID,以便在remaining查询中排除随机查询之后运行的内容。

你需要两个循环,因为你不能“包括”3个特定的帖子,这些帖子显示在其余帖子的顶部。但是,您可以尝试使用一个循环来执行此操作,该循环以错误的顺序返回帖子,但您可以使用网格css来更改显示顺序。

SO网友:Rick Hellewell

可能循环中存在以下伪代码:

// set up a counter
// the WP Loop
   // if counter < 4 (so first three posts)
      // put the post output text into an array element (with title, links, content, etc)
   // if counter > 3 (so the next posts after the first three
      // put the post output into a different array element
   // increment the counter
// the WP Loop end
// now output the loop\'s content
// for the elements in the first array (the first three posts)
   // output each element in the loop (each element is the post title.content/etc
// end the first loop
// for the elements in the 2nd array (the rest of the post)
   // output each element in the loop
// end the second loop
// all done
很粗糙,但你可能会明白。

结束