【问题标题】:Pagination For Custom Query Is Not Working自定义查询的分页不起作用
【发布时间】:2015-04-29 19:46:20
【问题描述】:

我正在创建一个自定义主页模板,我试图在其中显示带有导航的帖子。

这是我所遵循的。

  1. 创建了一个文件 homepage.php 并将模板名称命名为“主页”。
  2. 从仪表板创建了一个页面“主页”并分配了此模板“主页”。
  3. 然后从设置>阅读,选择静态首页“首页”。

这是我查询帖子的代码。

  <div class="posts-container">
    <?php query_posts('post_type=post&posts_per_page=2&post_status=publish&paged='. get_query_var('paged')); ?>
     <?php if(have_posts()): ?>
      <?php while (have_posts() ) : the_post(); ?>
        <div class="post"> 
          <h4 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
          </div>
      <?php endwhile; // end of the loop. ?>
   </div>

    <div class="pagination-nav">
     <div class="alignleft"><?php next_posts_link(__('Next »','example')); ?></div>
     <div class="alignright"><?php previous_posts_link(__('« Previous','example')); ?></div>
    </div>
    <?php endif; ?>
    <?php wp_reset_postdata(); ?>

现在,在单击分页功能生成的链接时,我会被重定向到我最初所在的页面。当 URL 像

一样更新时

http://localhost/mysite/?paged=2

假设有 4 个帖子(帖子 1、帖子 2、​​帖子 3、帖子 4)和最近的帖子分别是帖子 4 和帖子 3,那么我无法到达帖子 1 和帖子 2。页面

http://localhost/mysite
http://localhost/mysite/?paged=2

只显示帖子 4 和帖子 3。我该怎么做才能解决这个问题?

【问题讨论】:

  • 不要使用query_posts。它打破了主要的查询和分页

标签: wordpress


【解决方案1】:

对于static front page,您还需要检查page 变量,方法如下:

if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }

query_posts('...&paged=' . $paged); 

【讨论】:

    【解决方案2】:

    这是对我有用的解决方案。我不得不重新编写查询,并且这次使用了 WP Query。

      <?php 
       global $wp_query, $paged; 
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
        $query_args = array (
              'post_type' => 'post',
              'post_status'=>'publish',
              'posts_per_page' => 4,      //for testing purposes
              'paged' => $paged,
        );
    
        $wp_query = new WP_Query($query_args);
    
    if ( $wp_query->have_posts() ) :
       while ( $wp_query->have_posts()) :
             $wp_query->the_post();
      ?>
    <div class="post">
      <?php if ( has_post_thumbnail() ) { ?>
      <a class="entry-image" href="<?php the_permalink(); ?>">
      <?php the_post_thumbnail(); } ?>
      </a>
      <h4 class="entry-title"><a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>
        </a></h4>
      <div class="entry">
        <?php the_excerpt(); ?>
      </div>
    </div>
    <?php endwhile; ?>
    <div class="pagination-nav">
      <div class="alignleft">
        <?php next_posts_link(__('Oudere artikelen')); ?>
      </div>
      <div class="alignright">
        <?php previous_posts_link(__('Newer')); ?>
            </div>
          </div>
        </div>
        <!-- /posts-container -->
        <?php endif; ?>
        <?php wp_reset_postdata(); ?>
    

    而且哇!,这个成功了。 可能对其他人有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      相关资源
      最近更新 更多