简体   繁体   English

将编号的分页添加到我的自定义WP查询中

[英]Add Numbered Pagination to My Custom WP Query

I do have this code in my News Page Template 我的新闻页面模板中确实有此代码

<?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(); ?>

I'm using the WP Navi Plugin, but it doesnt show any pagination in my News page. 我正在使用WP Navi插件,但在“新闻”页面中没有显示任何分页。 What am i missing here? 我在这里想念什么?

Thanks 谢谢

well, actually wp_pagenavi(); 好吧,实际上是wp_pagenavi(); uses global $wp_query variable to determine the pages and therefore show pagination. 使用全局$ wp_query变量确定页面并因此显示分页。

but for custom query like yours may not work. 但对于像您这样的自定义查询可能无法正常工作。 but you can help it do this way. 但是您可以通过这种方式帮助它。

<?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 ?>

Hope this helped!! 希望这有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM