我正在使用与此类似的代码question, 仅当我在页面上显示所有条款时,按发布日期订购条款才有效,例如terms_to_display = 99999
, 但是,如果我将其设置为6,或者例如,完整的循环顺序被打乱,它不会在下一页继续,它会再次从最新的帖子开始(减去偏移量6),并按日期显示接下来的6篇帖子,结果是在第二、第三页有新的帖子。。。页面比第一页多。
$taxonomy = \'ctc_sermon_series\'; //taxonomy name
$terms_to_display = 6; //number of series to display per page
$page = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$totalterms = wp_count_terms( $taxonomy, array( \'hide_empty\' => TRUE ) );
$totalpages = ceil( $totalterms / $number );
$posts_per_term = 1; // number of posts per term to display
$term_count = get_terms( array (
\'taxonomy\' => $taxonomy,
\'fields\' => \'count\',
) );
// find out the number of pages to use in pagination
$max_num_pages = ceil( $term_count / $terms_to_display );
// get the page number from URL query
$current_page = get_query_var( \'paged\', 1 );
// calculate the offset, if there is one.
$offset = 0; // initial
// or changed the if not the first (0)
if( ! 0 == $current_page) {
$offset = ( $terms_to_display * $current_page ) - $terms_to_display;
}
$terms = get_terms ( array (
\'taxonomy\' => $taxonomy,
\'hide_empty\' => true,
\'number\' => $terms_to_display,
\'offset\' => $offset,
)
);
$sermons_items = [];
foreach ( $terms as $term ) {
$args = array (
\'post_type\' => \'ctc_sermon\',
\'posts_per_page\' => $posts_per_term,
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'tax_query\' => array (
array (
\'taxonomy\' => $taxonomy,
\'field\' => \'id\',
\'terms\' => array( $term->term_id )
),
),
);
$sermons_query = new WP_Query( $args );
// ignore terms with not enough posts
if ( $sermons_query->post_count < $posts_per_term ) {
continue;
}
$sermons_posts = $sermons_query->get_posts();
// get date of newest post in this term
$newest_post_date = get_the_date( "U", $sermons_posts[0]->ID );
$sermons_items[$newest_post_date] = array(
\'term\' => $term,
\'sermons\' => $sermons_posts,
);
}
krsort( $sermons_items ); // sort descending by keys
$sermons_items = array_slice( $sermons_items, 0, $terms_to_display );
global $post;
echo \'<section class="grid-posts">\';
foreach ( $sermons_items as $item ) {
echo \'<div class="col-md-6 no-padding grid-post">\';
foreach ( $item[\'sermons\'] as $post ) {
setup_postdata( $post ); ?>
<div class="grid-post-content">
<a href="<?php echo esc_url( home_url( \'/sermon-series/\' ) ).$item[\'term\']->slug; ?>">
<h2 class="entry-title"><?php echo $item[\'term\']->name; ?></h2>
</a>
<?php
echo get_the_title().\'</br>\';
echo get_the_date();
?>
</div>
<?php
}
echo \'</div>\';
}
echo \'</section>\';
echo paginate_links( array (
\'total\' => $max_num_pages,
\'current\' => $current_page,
) );