我一直在使用以下代码:
$current_page = get_query_var(\'paged\');
$current_page = max( 1, $current_page );
$per_page = 12;
$offset_start = 1;
$offset = ( $current_page - 1 ) * $per_page + $offset_start;
$post_list = new WP_Query(array(
\'cat\' => -15,
\'posts_per_page\' => $per_page,
\'paged\' => $current_page,
\'offset\' => $offset, // Starts with the second most recent post.
\'orderby\' => \'date\', // Makes sure the posts are sorted by date.
\'order\' => \'DESC\', // And that the most recent ones come first.
));
// Manually count the number of pages, because we used a custom OFFSET (i.e.
// other than 0), so we can\'t simply use $post_list->max_num_pages or even
// $post_list->found_posts without extra work/calculation.
$total_rows = max( 0, $post_list->found_posts - $offset_start );
$total_pages = ceil( $total_rows / $per_page );
if ( $post_list->have_posts() ):
while ( $post_list->have_posts() ):
$post_list->the_post();
// loop output here
endwhile;
echo paginate_links( array(
\'total\' => $total_pages,
\'current\' => $current_page,
) );
endif;
wp_reset_postdata();
答案如下:
https://stackoverflow.com/questions/49227520/how-can-i-paginate-wp-query-results-when-theres-an-offset是否可以在这一行周围添加css类?
echo paginate_links( array(
\'total\' => $total_pages,
\'current\' => $current_page,
) );
所以它会是这样的:
<div class="col-lg-12">
echo paginate_links( array(
\'total\' => $total_pages,
\'current\' => $current_page,
) );
</div>
我所做的一切都导致了Wordpress错误页面
谢谢
最合适的回答,由SO网友:ScottM 整理而成
您可以做您想做的事情—向HTML div标记添加CSS类选择器,但您将HTML和PHP代码错误地混合在一起。为了正确执行此操作,您需要相应地使用PHP开始/结束标记,例如:
?>
<div class="col-lg-12">
<?php
echo paginate_links( array(
\'total\' => $total_pages,
\'current\' => $current_page,
) );
?>
</div>
<?php
或者,您也可以这样做(不需要PHP打开/关闭标记):
echo \'<div class="col-lg-12">\';
echo paginate_links( array(
\'total\' => $total_pages,
\'current\' => $current_page,
) );
echo \'</div>\';