自定义投递类型分页在存档页面上不起作用

时间:2016-04-02 作者:Peekay

在过去的3个小时里,我一直在尝试将分页显示在我的自定义帖子类型存档页面上。

我有点理解分页只适用于默认循环,一旦修改了循环,就会有点中断。

无论如何,我希望你能给我指出正确的方向。

<div class="row text-center small-up-1 medium-up-2 large-up-3 small_section">

  <?php $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1; ?>

  <!-- Arguments for Loop -->
  <?php $args = array(
    \'post_type\' => \'portfolio\',
    \'posts_per_page\' => 6,
    \'paged\' => $paged,    
  ); ?>

  <!-- Setting Up the Query -->
  <?php $work = new WP_Query( $args ); ?>

  <?php if ( $work->have_posts() ) : while ( $work->have_posts() ) : $work->the_post(); ?>
  <div class="columns"> 
    <div class="box relative">
      <a class="overlay" href="<?php the_permalink(); ?>">
        <div class="vertical_align">
          <h5><?php the_title();?></h5>
          <p>asdas, asd, asddasds</p>
        </div>
      </a>
     <?php the_post_thumbnail(); ?>
    </div>
  </div>
  <?php endwhile; ?>

  <!-- Pagination Goes Here -->
  <div class="row">
    <div class="small-12 columns">
    <?php
    the_posts_pagination( array(
      \'mid_size\'  => 2,
      \'prev_text\' => \'Previous\',
      \'next_text\' => \'Next\',
    ) );?>
    </div>
  </div>
  <?php wp_reset_postdata();
  else : ?>
  <p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
  <?php endif; ?>
</div>

1 个回复
最合适的回答,由SO网友:Jevuska 整理而成

在post type archive页面上修改主查询,而不是直接修改主查询,只需使用普通循环(您可以create post type template, 在你的情况下archive-portofolio.php ), 并在过滤器下运行修改查询pre_get_posts.

正常循环打开archive-portfolio.php

<div class="row text-center small-up-1 medium-up-2 large-up-3 small_section">
  <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <div class="columns"> 
    <div class="box relative">
      <a class="overlay" href="<?php the_permalink(); ?>">
        <div class="vertical_align">
          <h5><?php the_title();?></h5>
         <!-- YOUR ANOTHER STUFF -->
        </div>
      </a>
     <?php the_post_thumbnail(); ?>
    </div>
  </div>
  <?php endwhile; ?>

  <!-- Pagination Goes Here -->
  <div class="row">
    <div class="small-12 columns">
    <?php
        the_posts_pagination( array(
            \'mid_size\'  => 2,
            \'prev_text\' => \'Previous\',
            \'next_text\' => \'Next\',
        ) );
    ?>
    </div>
  </div>
  <?php else : ?>
  <p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
  <?php endif; ?>
</div>
在中修改查询帖子类型functions.php

add_action( \'pre_get_posts\' ,\'wpse222471_query_post_type_portofolio\', 1, 1 );
function wpse222471_query_post_type_portofolio( $query )
{
    if ( ! is_admin() && is_post_type_archive( \'portofolio\' ) && $query->is_main_query() )
    {
        $query->set( \'posts_per_page\', 6 ); //set query arg ( key, value )
    }
}

相关推荐

Add pagination to WP_Query

我试图在下面的代码中添加分页,但不知道如何与之集成。请帮忙在帖子上找到页码。<?php $child_query = new WP_Query(array(\'post_type\' => \'blogpost\', \'orderby\' => \'date\', \'order\' => \'DESC\')); while ( $child_query->ha