dtrz99313 2018-10-05 05:04 采纳率: 100%
浏览 67
已采纳

Wordpress自定义帖子类型分类视图与分页

I have a custom post type 'charters' and within my category.php page I have a loop pulling in 'charter' posts for the current category id. I have a custom pagination setup displaying page numbers when I click page 2 link, it goes to URL /page/2 which displays a 404 error. I can't figure out why I am getting the 404. If I adjust my posts per page the pagination updates and displays the correct number of pages but the links do not show up.

Category.php Code:

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

  $product_args = array(
      'post_type' => 'charters',
      'posts_per_page' => 10, //the same as the parse_query filter in our functions.php file
      'paged' => $paged,
      'page' => $paged,
      'cat' => $cat
    );

  $product_query = new WP_Query( $product_args ); ?>

  <?php if ( $product_query->have_posts() ) : ?>

    <!-- the loop -->
    <?php while ( $product_query->have_posts() ) : $product_query->the_post(); ?>
      <article class="loop">
        <h3><?php the_title(); ?></h3>
        <div class="content">
          <?php the_excerpt(); ?>
        </div>
      </article>
    <?php endwhile; ?>
    <!-- end of the loop -->


    <!-- pagination here -->
    <?php
       if (function_exists( 'custom_pagination' )) :
          custom_pagination( $product_query->max_num_pages,"",$paged );
      endif;
   ?>


  <?php wp_reset_postdata(); ?>

  <?php else:  ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
  <?php endif; ?>

Functions.php Code:

function prefix_change_cpt_archive_per_page( $query ) {

    //* for cpt or any post type main archive
    if ( $query->is_main_query() && ! is_admin() && is_post_type_archive( 'product' ) ) {
        $query->set( 'posts_per_page', '10' );
    }

}
add_action( 'pre_get_posts', 'prefix_change_cpt_archive_per_page' );


function prefix_change_category_cpt_posts_per_page( $query ) {

    if ( $query->is_main_query() && ! is_admin() && is_category( 'test-category' ) ) {
        $query->set( 'post_type', array( 'product' ) );
        $query->set( 'posts_per_page', '2' );
    }

}
add_action( 'pre_get_posts', 'prefix_change_category_cpt_posts_per_page' );



function custom_pagination( $numpages = '', $pagerange = '', $paged='' ) {

  if (empty($pagerange)) {
    $pagerange = 2;
  }


  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }


  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('&laquo;'),
    'next_text'       => __('&raquo;'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
    echo "<nav class='custom-pagination'>";
      echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
      echo $paginate_links;
    echo "</nav>";
  }

My Permalink Settings are set to "Post Name" I have a feeling this has something to do with rewrites but I can not figure out how to get the page urls to work correctly. An example of my categories are as follows:

/category/long-beach/
/category/san-diego/

  • 写回答

3条回答 默认 最新

  • douqian3712 2018-10-05 21:27
    关注

    There was a couple of errors in my code. This is what I get for copy/pasting code and not going through it line by line. Thanks everyone who helped out, but I got this to work by changing:

    Bad Code:

    function prefix_change_category_cpt_posts_per_page( $query ) {
    
        if ( $query->is_main_query() && ! is_admin() && is_category( 'test-category' ) ) {
            $query->set( 'post_type', array( 'product' ) );
            $query->set( 'posts_per_page', '2' );
        }
    
    }
    

    Good Code:

    function prefix_change_category_cpt_posts_per_page( $query ) {
    
        if ( $query->is_main_query() && ! is_admin() && is_category( $cat ) ) {
            $query->set( 'post_type', array( 'charters' ) );
            $query->set( 'posts_per_page', '10' );
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效