类别查询上方的粘帖

问题描述 投票:0回答:1

我在首页上使用此查询来显示wp_query中特定类别的三个帖子。查询中的文章是随机显示的。

现在,粘性的帖子希望保留在顶部,其余两篇文章仍将随机分发。 wp_query有可能吗?

此代码段仅返回一个粘性帖子,其余文章不显示。为什么?


           <?php

$args = array(
      'cat'=>'11, 12,20,24',
       'posts_per_page'=> '3',
       'post__in' => get_option( 'sticky_posts' ),
       'ignore_sticky_posts' => 1,
       'orderby'=> 'rand'
);

// Custom query.
$query = new WP_Query( $args );

// Check that we have query results.
if ( $query->have_posts() ) {

    // Start looping over the query results.
    while ( $query->have_posts() ) {

        $query->the_post();

        // Contents of the queried post results go here.

    }

}

// Restore original post data.
wp_reset_postdata();

?>
wordpress random categories sticky
1个回答
0
投票

这里您可以使用两个查询1)用于粘性帖子

$args = array(
   'cat'=>'11, 12,20,24',
   'posts_per_page'=> '1',
   'post__in' => get_option( 'sticky_posts' ),
   'ignore_sticky_posts' => 1,
   'orderby'=> 'rand'
);

2)其他职位

$args = array(
   'cat'=>'11, 12,20,24',
   'posts_per_page'=> '2',
   'ignore_sticky_posts' => 1,
   'orderby'=> 'rand'
);
© www.soinside.com 2019 - 2024. All rights reserved.