【问题标题】:Pagination don't work in custom page template分页在自定义页面模板中不起作用
【发布时间】:2014-04-03 00:04:31
【问题描述】:

我创建了一个自定义页面模板,例如 category.php,但我无法让分页工作。 我只是从一个类别中过滤帖子。

这是我的代码:

<?php
/* Template Name: News */
?>

<?php get_header(); ?>

<div class="col-lg-9 col-md-8 content">

<div class="box">
    <h1 class="title"><?php the_title(); ?></h1>
    <div class="box-int">
      <article>

        <?php // Display blog posts on any page @ http://m0n.co/l


        $temp = $wp_query; $wp_query= null;
        $wp_query = new WP_Query(); $wp_query->query('showposts=5' . '&paged='.$paged);
        while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

        <?php 
        if (has_post_thumbnail()) {
          the_post_thumbnail();
        }
        ?>

        <h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
        <?php the_excerpt(); ?>
        <br><br>
        <?php endwhile; ?>

        <?php if ($paged > 1) { ?>

        <nav id="nav-posts">
          <div class="prev"><?php next_posts_link('&laquo; Old'); ?></div>
          <div class="next"><?php previous_posts_link('Newx &raquo;'); ?></div>
        </nav>

        <?php } else { ?>

        <nav id="nav-posts">
          <div class="prev"><?php next_posts_link('&laquo; Old'); ?></div>
        </nav>

        <?php } ?>

        <?php wp_reset_postdata(); ?>

      </article>
    </div>
</div>
</div>

<div class="col-lg-3 col-md-4">
  <?php get_sidebar(); ?>
</div>

<?php get_footer(); ?>

这里有什么问题?我点击第2页,但帖子总是一样。

【问题讨论】:

    标签: wordpress pagination


    【解决方案1】:

    您的查询在这里有点乱,还有一些语法错误。showposts 与恐龙同归于尽,它已经贬值了很长时间。你应该使用posts_per_page'showposts=5' . '&amp;paged='.$paged 也是一团糟。你查询应该简化看起来像这样

    $args = array(
        'posts_per_page' => 5,
        'paged' => $paged
    );
    $query = new WP_Query( $args );
     while ($query->have_posts()) : $query->the_post(); ?>
    

    使用WP_Query查询循环时,需要将$max_pages参数赋值给next_posts_link()

    所以您需要将&lt;?php next_posts_link('&amp;laquo; Old'); ?&gt; 更改为&lt;?php next_posts_link('&amp;laquo; Old', $the_query-&gt;max_num_pages ); ?&gt;

    还可以阅读 this question 关于此主题的内容。如果我遗漏了什么,这个问题肯定会对你有很大帮助。

    【讨论】:

    • 没用。链接发送到 page/2/ 但帖子始终相同。
    • @OsnyNet 检查更新。我提供的链接将为您提供很大帮助。
    • 我什至尝试了上面的代码“把它放在一起”,但无法工作。
    • @OsnyNetto 你的查询一团糟,请查看我的更新
    猜你喜欢
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 2015-03-20
    相关资源
    最近更新 更多