WordPress分页链接始终指向主页

时间:2017-05-04 作者:fatg

我正在使用自定义查询对此网站进行分页,并get_next_posts_link, get_previous_posts_link. 问题是链接到旧条目(get_next_posts_link) 只工作一次,这意味着如果我第二次单击它,它将始终指向主页,这很奇怪,因为当我检查链接时,href属性是:http://localhost:8888/athena/event/page/3.

根据变量,共有7页$queryObject->max_num_pages

显示我的意思的小屏幕捕获视频(27秒长):https://www.useloom.com/share/f8f9ecac9dd54a49aa3613f9c0f5c9f9

这是我的代码:

    <!-- section list events-->
    <?php
      if(get_query_var(\'paged\')){
        $paged = get_query_var(\'paged\');
      } elseif (get_query_var(\'page\')) {
        $paged = get_query_var(\'page\');
      } else {
        $paged = 1;
      }
      $query_args = array(
        \'post_type\' => \'event\',
        \'posts_per_page\' => 3,
        \'paged\' => $paged
      );
      $queryObject = new WP_Query($query_args);
     ?>

    <section class="block-list-events">
      <div class="container">
        <div style="color: #000;">
          <?php var_dump($queryObject->found_posts); ?>
        </div>

        <div class="list-events">
        <?php if ($queryObject->have_posts()): while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
          <div class="item clearfix">
            <div class="img tbl pull-left">
              <div class="tbl-cell date">
                <p><?php the_time(\'Y M\') ?></p>
                <p><span><?php the_time(\'j\') ?></span></p>
              </div>
              <div class="tbl-cell img-a">
                <a href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url(\'event-single\'); ?>" width="530" height="300" alt="<?php the_title(); ?>"/></a>
              </div>
            </div>
            <div class="info pull-left">
              <p class="tag"><?php the_field(\'label\'); ?></p>
              <h4><a href="<?php echo get_the_permalink() ?>"><?php the_title(); ?></a></h4>
              <p class="desc"><?php echo excerpt(25); ?></p>
              <div class="button-view-detail">
                <a class="btn btn-3" href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>">View Details</a>
              </div>
            </div>
          </div>
         <?php endwhile; ?>
        </div>
        <?php endif; ?>

        <div class="clearfix">
      <!-- Pagination -->
      <?php if ($queryObject->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
        <nav class="prev-next-posts">
          <div class="prev-posts-link">
            <?php echo get_next_posts_link( \'Older Entries\', $queryObject->max_num_pages ); // display older posts link ?>
          </div>
          <div class="next-posts-link">
            <?php echo get_previous_posts_link( \'Newer Entries\', $queryObject->max_num_pages ); // display newer posts link ?>
          </div>
        </nav>
      <?php } ?>
    </div>

    </section>
    <!-- /end of section list events -->
  </main>

<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>
根据@amit的建议,我已经更新了代码,但结果仍然与之前一样:

    <!-- section list events-->
    <?php

      if(get_query_var(\'paged\')){
        $paged = get_query_var(\'paged\');
      } elseif (get_query_var(\'page\')) {
        $paged = get_query_var(\'page\');
      } else {
        $paged = 1;
      }
      $query_args = array(
        \'post_type\' => \'event\',
        \'numberposts\' => -1,
        \'posts_per_page\' => 3,
        \'paged\' => $paged
      );
      $queryObject = new WP_Query($query_args);

      // Pagination fix
      $temp_query = $wp_query;
      $wp_query   = NULL;
      $wp_query   = $queryObject;
     ?>

    <section class="block-list-events">
      <div class="container">
        <div style="color: #000;">
          <?php var_dump($queryObject->found_posts); ?>
        </div>

        <div class="list-events">
        <?php if ($queryObject->have_posts()): while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
          <div class="item clearfix">
            <div class="img tbl pull-left">
              <div class="tbl-cell date">
                <p><?php the_time(\'Y M\') ?></p>
                <p><span><?php the_time(\'j\') ?></span></p>
              </div>
              <div class="tbl-cell img-a">
                <a href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url(\'event-single\'); ?>" width="530" height="300" alt="<?php the_title(); ?>"/></a>
              </div>
            </div>
            <div class="info pull-left">
              <p class="tag"><?php the_field(\'label\'); ?></p>
              <h4><a href="<?php echo get_the_permalink() ?>"><?php the_title(); ?></a></h4>
              <p class="desc"><?php echo excerpt(25); ?></p>
              <div class="button-view-detail">
                <a class="btn btn-3" href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>">View Details</a>
              </div>
            </div>
          </div>
         <?php endwhile; ?>
        </div>
        <?php endif; ?>

    <?php
      // Reset postdata
      wp_reset_postdata();
    ?>

        <div class="clearfix">
      <!-- Pagination -->
      <?php if ($queryObject->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
        <nav class="prev-next-posts">
          <div class="prev-posts-link">
            <?php echo get_next_posts_link( \'Older Entries\', $queryObject->max_num_pages ); // display older posts link ?>
          </div>
          <div class="next-posts-link">
            <?php echo get_previous_posts_link( \'Newer Entries\' ); // display newer posts link ?>
          </div>
        </nav>
      <?php } ?>
    </div>

    </section>
    <!-- /end of section list events -->
  </main>
<?php
  // Reset main query object
  $wp_query = NULL;
  $wp_query = $temp_query;
?>
<?php get_footer(); ?>

2 个回复
SO网友:BlueSuiter
<!-- section list events-->
<?php

  if(get_query_var(\'paged\')){
    $paged = get_query_var(\'paged\');
  } elseif (get_query_var(\'page\')) {
    $paged = get_query_var(\'page\');
  } else {
    $paged = 1;
  }
  $query_args = array(
    \'post_type\' => \'event\',
    \'numberposts\' => -1,
    \'posts_per_page\' => 3,
    \'paged\' => $paged
  );
  $queryObject = $wp_query;
  $queryObject = new WP_Query($query_args);

 ?>

<section class="block-list-events">
  <div class="container">
    <div style="color: #000;">
      <?php var_dump($queryObject->found_posts); ?>
    </div>

    <div class="list-events">
    <?php if ($queryObject->have_posts()): while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
      <div class="item clearfix">
        <div class="img tbl pull-left">
          <div class="tbl-cell date">
            <p><?php the_time(\'Y M\') ?></p>
            <p><span><?php the_time(\'j\') ?></span></p>
          </div>
          <div class="tbl-cell img-a">
            <a href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url(\'event-single\'); ?>" width="530" height="300" alt="<?php the_title(); ?>"/></a>
          </div>
        </div>
        <div class="info pull-left">
          <p class="tag"><?php the_field(\'label\'); ?></p>
          <h4><a href="<?php echo get_the_permalink() ?>"><?php the_title(); ?></a></h4>
          <p class="desc"><?php echo excerpt(25); ?></p>
          <div class="button-view-detail">
            <a class="btn btn-3" href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>">View Details</a>
          </div>
        </div>
      </div>
     <?php endwhile; ?>
    </div>
    <?php endif; ?>

    <div class="clearfix">
  <!-- Pagination -->
  <?php if ($queryObject->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
    <nav class="prev-next-posts">
      <div class="prev-posts-link">
        <?php echo get_next_posts_link( \'Older Entries\', $queryObject->max_num_pages ); // display older posts link ?>
      </div>
      <div class="next-posts-link">
        <?php echo get_previous_posts_link( \'Newer Entries\' ); // display newer posts link ?>
      </div>
    </nav>
  <?php } ?>
</div>

<?php
  // Reset postdata
  wp_reset_postdata();
?>

</section>
<!-- /end of section list events -->

SO网友:Mark Kaplun

分页不太可能处理任何不是主查询的内容,也没有太多的理由试图强制它在其他地方工作。使用pre_get_posts 如果修改主查询将获得良好的结果,请进行筛选,否则只需创建自己的分页方案,而不使用与wordpress相同的参数。

相关推荐

在Get_the_Posts_Pagination函数中编辑分页文本

我想在链接模板中编辑screen\\u reader\\u文本。php我可以在一个主题中这样做,这样它就不会在更新时被覆盖。看起来过滤器是最好的选择,但我找不到关于使用什么过滤器的文档。这是我想从链接模板更改的代码。php: if ( $GLOBALS[\'wp_query\']->max_num_pages > 1 ) { $args = wp_parse_args( $args, array( \'mid_size\' =&