添加orderby
和order
针对您的论点:
$cat_query = new WP_Query( array(
\'post__not_in\' => get_option( \'sticky_posts\' ),
\'category__in\' => array($cat->term_id),
\'posts_per_page\' => 5,
\'paged\' => $paged,
\'orderby\' => \'date\',
\'order\' => \'DESC\'
) );
Update似乎它仍然需要一个类别,列出5个帖子,然后转到另一个类别,有5个帖子等等。它需要最后的帖子,但它们是一个又一个类别列出的
请尝试以下操作:
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$i = 0;
if ( $cats = get_categories() ) foreach( $cats as $cat ) :
$cat_query = new WP_Query(
array(
\'post__not_in\' => get_option( \'sticky_posts\' ),
\'category__in\' => array ( $cat->term_id ),
\'posts_per_page\' => 5,
\'paged\' => $paged,
\'orderby\' => \'date\',
\'order\' => \'DESC\'
)
);
if ( $cat_query->have_posts() ) : ?>
<?php while ( $cat_query->have_posts() ) : $cat_query->the_post();
// arrays of all the posts\' IDs and dates
$the_posts[\'ID\'][$i] = $post->ID;
$the_posts[\'date\'][$i] = $post->post_date;
$i++;
endwhile;
endif; wp_reset_postdata();
endforeach;
foreach ( $the_posts[\'date\'] as $the_post_date ) {
$post_dates[] = $the_post_date;
}
// sort all the posts by their dates
array_multisort( $post_dates, SORT_DESC, $the_posts[\'ID\'] );
foreach ( $the_posts[\'ID\'] as $the_post_id ) {
$post_ids[] = $the_post_id;
}
$query = new WP_Query( array( \'post_type\' => \'post\', \'post__in\' => $post_ids ) );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<h3 class="cat-title"><?php $cat = get_the_category( get_the_ID() ); echo $cat[0]->name; ?></h3>
<?php
get_template_part( \'loop\', \'index\' );
endwhile;
endif; wp_reset_postdata();