【问题标题】:How i can enable WordPress Posts Pagination我如何启用 WordPress 帖子分页
【发布时间】:2015-07-02 17:49:31
【问题描述】:

如何在我的页面中启用分页(模板页面 wordpress)

我的代码

<?php
$catquery = new WP_Query( 'cat=2&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
  <br />
<div class="news"><!-- Start News Box -->
<div class="img_news"><!-- Start Image News -->
<?php
$url_thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<img class="img_thumbs" title="" alt="" src="<?php echo $url_thumb; ?>">
</div><!-- End Image News -->
<div class="title_news"><!-- Start Title News -->
<h2>
<?php the_title(); ?>
</h2>
<div class="details">
<?php the_content_limit(500, "Read More..."); ?>
</div>
</div><!-- End Title News -->
<hr>
</div><!-- End News Box -->
</div>
<?php endwhile; ?>

我正在使用它,但我看不到分页栏:示例 (1-2-3-...-100)

谢谢

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    你需要在参数($catquery)中添加'paged'属性。

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    $catargs = array('cat'=>'2','posts_per_page'=>10,'paged' => $paged);
    
    $catquery = new WP_Query( $catargs);
    while($catquery->have_posts()) : $catquery->the_post();
    //do stuff
    endwhile;
    
    $big = 999999999; 
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $catquery->max_num_pages,
        'prev_text'          => __( 'Previous page', 'twentyfifteen' ),
        'next_text'          => __( 'Next page', 'twentyfifteen' ),
    ) ); 
    

    【讨论】:

      【解决方案2】:

      您应该在循环之前或之后(while 循环之前或之后)添加这些分页函数:

      <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
      <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
      

      查看 Wordpress 的文档 here

      【讨论】:

        猜你喜欢
        • 2013-01-23
        • 1970-01-01
        • 2013-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多