douxiuar885064 2014-10-10 01:40
浏览 65
已采纳

使用“wp-query”使用Wordpress进行分页

Hey all i'm running a query loop in a custom page.php using a custom post type. I would for it to paginated so i could have a specific amount of post per page. I was wondering if someone could help me out. I have added my code below:

<?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; ?>

Thanks!

  • 写回答

1条回答 默认 最新

  • dongxiang3648 2014-10-10 01:50
    关注

    The problem as wordpress.org documentation says:

    Pagination won't work correctly, unless you set the 'paged' query var appropriately: adding the paged parameter

    Example of usage:

    <?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
    ?>
    

    Also you can check rvoodoo guide if you have more questions about it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog