将编号分页添加到我的自定义WP_Query

时间:2013-12-27 08:30:11

标签: php wordpress pagination

我的新闻页面模板中有这段代码

<?php $mcc_query = new WP_Query('showposts=3'); 
    while($mcc_query->have_posts()) : $mcc_query->the_post(); ?>

<div class="main-news-cont">
  <div class="news-feat fl"></div>
  <div class="news-content-cont fr">
    <div class="news-title"><a href="<?php the_permalink(); ?> ">
      <?php the_title( '<h2>', '</h2>' ); ?>
      </a></div>
    <div class="news-meta"><?php echo get_the_date('j F, Y'); ?>, <?php echo get_the_time(); ?> Written by <a href="">
      <?php the_author(); ?>
      </a></div>
    <div class="news-excerpt">
      <?php the_excerpt(); ?>
    </div>
    <a href="<?php the_permalink(); ?> " id="readmore-news">Read More</a> </div>
  <div class="clearfix"></div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php wp_pagenavi(); ?>

我正在使用WP Navi插件,但它在我的新闻页面中没有显示任何分页。 我在这里缺少什么?

由于

1 个答案:

答案 0 :(得分:0)

好吧,实际上是wp_pagenavi();使用全局$ wp_query变量来确定页面,从而显示分页。

但是对于像您这样的自定义查询可能无效。但是你可以帮助它这样做。

<?php 
global $wp_query; 
$old_query=$wp_query; // backup $wp_query so we can roll it back later
?>
<?php $wp_query= new WP_Query('showposts=3'); // use $wp_query instead of your own variable so every other stuffs work as expected in the loop ?>
    <?php while($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <?php /* your post loop content */ ?>
    <?php endwhile; ?>
    <?php wp_pagenavi(); // show the pagination pagination ?>
    <?php wp_reset_postdata(); wp_reset_query(); //roll back query vars to as per the request ?>
<?php $wp_query=$old_query; // revert back the $wp_query to as it was before ?>

希望这有帮助!!

相关问题