Wordpress使用" wp-query"进行分页

时间:2014-10-10 01:41:00

标签: php wordpress post pagination wp-query

嘿所有我使用自定义帖子类型在自定义page.php中运行查询循环。我想它分页,所以我可以有每页特定数量的帖子。我想知道是否有人可以帮助我。我在下面添加了我的代码:

<?php query_posts( array(
     'post_type' => array( 'POSTTYPE' ),
      'showposts' => -1 )
     ); ?>

            <?php while ( have_posts() ) : the_post(); ?>


         <div class="">
         <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
         <?php the_post_thumbnail( 'thumbnail' , array('class' => 'aligncenter project_post_thumbnail') ); ?> 
         <a href="<?php the_permalink();?>">View </a>
         </div>


<?php endwhile; ?>

谢谢!

1 个答案:

答案 0 :(得分:0)

问题wordpress.org documentation说:

  

分页无法正常工作,除非您正确设置'paged'查询var:添加分页参数

使用示例:

<?php
 $args = array(
               'cat' => '5',
               'post_type' => 'POSTTYPE',
               'posts_per_page' => 6,
               'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
               );

query_posts($args);
while (have_posts()) : the_post();
 /* Do whatever you want to do for every page... */
?>
     <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
     <?php the_post_thumbnail( 'thumbnail' , array('class' => 'aligncenter project_post_thumbnail') ); ?> 
     <a href="<?php the_permalink();?>">View </a>
<?php
    endwhile;
?>
<div class="navigation">
    <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
    <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
</div>
<?php
    wp_reset_query();  // Restore global post data
?>

如果您对此有更多疑问,也可以查看rvoodoo guide