分页在自定义页面模板中不起作用

时间:2014-04-03 00:04:31

标签: wordpress pagination

我创建了一个自定义页面模板,例如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页,但帖子总是一样的。

1 个答案:

答案 0 :(得分:0)

你的查询在这里有些混乱,但也有一些语法错误。showposts死于恐龙,很长时间都在折旧。您应该使用posts_per_page'showposts=5' . '&paged='.$paged也是一团糟。你查询shouls简化看起来像这样

$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()

因此您需要将<?php next_posts_link('&laquo; Old'); ?>更改为<?php next_posts_link('&laquo; Old', $the_query->max_num_pages ); ?>

另请阅读this question关于此主题的内容。如果我错过了什么,这个问题肯定会帮助你很多。

相关问题