Wordpress:自定义分页查询

时间:2015-11-26 01:41:59

标签: wordpress wp-query

我曾经使用wordpress这个查询来获取我的帖子类型分页

  <?php 
      $paged = get_query_var('paged') ? get_query_var('paged') : 1;
      $args = array(
                     'post_type' => 'post',
                     'posts_per_page' => -1,
                     'paged' => $paged,
                     'author' => $user_id
                  );
      $my_query = new WP_Query($args);

      if($my_query->have_posts()):
          while($my_query->have_posts()):$my_query->the_post();

    ?>
      <article class="listaNoticia column large-6 medium-6 small-12">
        <a href="<?php the_permalink(); ?>">
          <p class="data"><?php the_time('d/m/Y'); ?></p> <br><br>
          <h2><?php the_title(); ?></h2>
        </a>
      </article>
      <!-- END ITEM -->
    <?php endwhile; endif; ?>


<div class="paginacao column large-12 medium-12 snall-12">
      <div>
        <?php wp_pagenavi(); ?>
        <?php if(function_exists('wp_paginate')) { wp_paginate(); } ?>
      </div>
    </div>

我曾经使用过此查询,但现在它加载了所有帖子而没有分页。

有人遇到这个问题吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

我不确定为什么之前让它工作,但'posts_per_page' => -1应该禁用分页。尝试不设置或使用您想要的值。

答案 1 :(得分:-2)

  • 在仪表板中 - &gt;设置 - &gt;阅读,你设置显示的博客页面最多显示多少? 10?您应该将'posts_per_page'设置为相同。 或者您可以尝试以下代码:

<?php

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
	'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
	'format' => '?paged=%#%',
	'current' => max( 1, get_query_var('paged') ),
	'total' => $my_query->max_num_pages
) );
?>

相关问题