【问题标题】:Pagination with Wordpress using "wp-query"使用“wp-query”使用 Wordpress 进行分页
【发布时间】:2014-10-10 01:40:59
【问题描述】:

大家好,我正在使用自定义帖子类型在自定义 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; ?>

谢谢!

【问题讨论】:

    标签: php post pagination wordpress


    【解决方案1】:

    wordpress.org documentation 说的问题:

    分页将无法正常工作,除非您适当地设置“分页”查询变量:添加分页参数

    使用示例:

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

    【讨论】:

    • 您好 Mitogh,它不起作用,我的帖子类型没有显示任何内容。我已经两次和三次检查自定义帖子类型名称是否正确。你在这里是什么意思:/* 为每一页做任何你想做的事情... */
    • 我刚刚搜索了一个“猫”是类别,我没有为该自定义帖子类型设置任何类别。所以我只是删除了那部分。再次感谢 Mitogh,我很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    相关资源
    最近更新 更多