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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀