对于一个新的WordPress模板,我编写了一个运行良好的循环。但是没有分页。有人看到了吗,为什么没有显示页码?谢谢
我的循环:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'numberposts' => -1,
'posts_per_page' => 9,
'paged' => $paged,
'cat' => '4'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
......
<?php
}
} else { echo 'no posts found'; }
wp_reset_postdata();
?>
在页面底部:
<?php the_posts_pagination( array( 'mid_size' => 2 ) ); ?>
请尝试一下。
if ( ! function_exists( 'pagination' ) ) :
function pagination( $paged = '', $max_page = '' ){
global $wp_query
$big = 999999999; // need an unlikely integer
if( ! $paged )
$paged = get_query_var('paged');
if( ! $max_page )
$max_page = $wp_query->max_num_pages;
echo paginate_links( array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link( $big ))),
'format' => '?paged=%#%',
'current' => max( 1, $paged ),
'total' => $max_page,
'mid_size' => 2,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'list'
) );
}
endif;
循环模板将类似于-
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $args = array(
''post_type' => 'post',
'posts_per_page' => 9,
'paged' => $paged,
'cat' => '4'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
pagination($paged, $loop->max_num_pages);// Pagination Function
endif;
wp_reset_postdata();