如何对此自定义循环进行分页?

时间:2015-09-25 作者:Paulo Gabriel

伙计们!我很确定这对你们来说是小菜一碟,但我正在尝试将分页添加到我的循环中。我尝试了一些可用的代码,并对其分页,但第2页、第3页等的内容与第1页的内容相同。目前我拥有的最佳解决方案是什么?

<?php
    $recentPosts = new WP_Query();
    $recentPosts->query(\'showposts=5\');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <article>
    </article>
<?php endwhile; ?>
提前谢谢。

3 个回复
SO网友:amespower

这可能是因为您使用的是自定义页面模板。请尝试以下操作。我已经对整个过程中的步骤进行了评论。希望有帮助。

<?php 
  //get the current page
  $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;

  //pagination fixes prior to loop
  $temp =  $query;
  $query = null;

  //custom loop using WP_Query
  $query = new WP_Query( array( 
    \'post_status\' => \'publish\',
    \'orderby\' => \'date\',
    \'order\' => \'ASC\' 
  ) ); 

 //set our query\'s pagination to $paged
 $query -> query(\'post_type=post&posts_per_page=5\'.\'&paged=\'.$paged);

 if ( $query->have_posts() ) : 
   while ( $query->have_posts() ) : $query->the_post();
    ?>
      <li>
        <?php if ( has_post_thumbnail()) : ?>
          <?php the_post_thumbnail();?>
        <?php endif; ?>
        <div class="someclass" >
            <h2><?php the_title(); ?></h2> 
            <?php the_content(); ?>
        </div> 
      </li>
  <?php endwhile;?>

  <?php //pass in the max_num_pages, which is total pages ?>
  <div class="pagenav">
    <div class="alignleft"><?php previous_posts_link(\'Previous\', $query->max_num_pages) ?></div>
    <div class="alignright"><?php next_posts_link(\'Next\', $query->max_num_pages) ?></div>
  </div>

<?php endif; ?>

<?php //reset the following that was set above prior to loop
$query = null; $query = $temp; ?>

SO网友:shanebp

有几种方法可以做到pagination. 这里有一个:

<?php $query = new WP_Query( array( \'posts_per_page\' => 5 ) ); ?>

<?php while ($query->have_posts()) : $query->the_post(); ?>
   <article>
      <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
   <article>
<?php endwhile; ?>

<div class="nav-previous alignleft"><?php next_posts_link( \'Older posts\' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( \'Newer posts\' ); ?></div>
当然,如果您的WP\\u查询没有正确构建,那么它将不起作用。

SO网友:Sindhu

这里我的帖子类型是“news\\u events”,每显示4篇帖子标题后就会显示分页。

$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$news_events = array( \'post_type\' => \'news_events\', \'posts_per_page\' => 4, \'paged\' => $paged);
$wp_query = new WP_Query( $news_events );
if ( $wp_query->have_posts() ) : ?>
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
          <?php the_title(); echo "<br/>"; ?>
    <?php endwhile; ?>
    <nav>
      <?php previous_posts_link(\'&laquo; Newer\',$wp_query->max_num_pages); ?>
      <?php next_posts_link(\'Older &raquo;\',$wp_query->max_num_pages); ?>
    </nav>
    <?php wp_reset_postdata(); ?>                           
    <?php else : ?>
        <p><?php _e( \'Sorry, no news events posts at this time.\', \'theme\' ); ?></p>
<?php endif; ?>

相关推荐

Custom pagination structure

当前我的分页工作方式如下http://example.com/city/usa/ #for page 1 http://example.com/city/usa/page/2 http://example.com/city/usa/page/3 ...and so on 我想将其更改为:http://example.com/city/usa/ #for page 1 http:/