我在自定义页面模板中添加了分页,但它不会显示。 如果我将此代码用作自定义类别模板 - 它适用于分页。怎么了?
我通过 WordPress 设置控制每页的帖子数量。如果我在循环中设置静态它也可以工作,但在任何配置中都没有分页。
这是我的代码:
<?php
get_header(); ?>
<div id="main-content" class="main-content">
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<div class="press-cat">
<?php
$category = get_the_category();
$current_category_ID = isset($category->cat_ID) ? $category->cat_ID : 0;
$args = array(
'taxonomy' => 'category',
'hide_empty' => 0,
'hierarchical' => 1,
'child_of' => 420,
'title_li' => 0,
'current_category' => $current_category_ID
);
wp_list_categories($args);
?>
</div>
<?php
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>0)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<div class="press-items">
<ul>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<li>
<div class="press-date">
<a href="<?php the_permalink(); ?>">
<?php $post_date = get_the_date( 'F j, Y' ); echo $post_date; ?>
</a>
</div>
<div class="press-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<div>
</li>
<?php endwhile; ?>
<!-- end of the loop -->
</ul>
</div>
<?php // Wordpress Pagination
$big = 999999999; // need an unlikely integer
$links = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => '<',
'next_text' => '>',
'type' => 'array'
) );
if(!empty($links)){ ?>
<ul class="pagination">
<?php
foreach($links as $link){
?>
<li><?php echo $link; ?></li>
<?php
}
wp_reset_query(); ?>
</ul>
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
</div><!-- #main-content -->
<?php
get_footer();
答案 0 :(得分:2)
您必须在 WP_Query 语句中定义“posts_per_page”,正如我所看到的,您已经定义了零 (0)
请设置为 10 或 20 或 30 等
$wpb_all_query->max_num_pages 变量而不是 $wp_query->max_num_pages 在您的分页代码中,因为您的查询存储在代码示例中的 $wpb_all_query 变量中
使用此代码进行分页
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wpb_all_query->max_num_pages
) );
请在您的查询代码中添加这个新参数
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
所以你的查询应该是这个
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>0, 'paged' => get_query_var('paged') ? get_query_var('paged') : 1));