categories - Unable to create pagination for Category.php

I'm trying to make a pagination for my category.php. My posts are custom post type.I get a 404 not found error whe

I'm trying to make a pagination for my category.php. My posts are custom post type.
I get a 404 not found error when I'm trying to get to the second page. I realised that this is happening only for category.php and not another page/template.

The code is:

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

                $args = array(
                'post_type' => 'products',
                'post_status' => 'publish',
                'cat' => $cat,
                'posts_per_page' => 20,
                'paged' => $paged
            );

            $query = new WP_Query( $args );

            if ( $query->have_posts() ) :              
                while ( $query->have_posts() ) : $query->the_post();?>
                    <a href="<?php the_permalink();?>">
                        <div class="product">
                            <div class="image-container">
                                <?php the_post_thumbnail();?>
                            </div> 
                            <p class="title"><?php the_title(); ?></p>
                        </div>    
                    </a>                   
                <?php 
                endwhile;?> 
                <div class="pagination-container">
                    <div class="pagination">
                        <?php 
                        echo paginate_links(array(
                            'total' => $query->max_num_pages
                        ));?>
                    </div>
                </div>

            <?php endif;?>

I'm trying to make a pagination for my category.php. My posts are custom post type.
I get a 404 not found error when I'm trying to get to the second page. I realised that this is happening only for category.php and not another page/template.

The code is:

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

                $args = array(
                'post_type' => 'products',
                'post_status' => 'publish',
                'cat' => $cat,
                'posts_per_page' => 20,
                'paged' => $paged
            );

            $query = new WP_Query( $args );

            if ( $query->have_posts() ) :              
                while ( $query->have_posts() ) : $query->the_post();?>
                    <a href="<?php the_permalink();?>">
                        <div class="product">
                            <div class="image-container">
                                <?php the_post_thumbnail();?>
                            </div> 
                            <p class="title"><?php the_title(); ?></p>
                        </div>    
                    </a>                   
                <?php 
                endwhile;?> 
                <div class="pagination-container">
                    <div class="pagination">
                        <?php 
                        echo paginate_links(array(
                            'total' => $query->max_num_pages
                        ));?>
                    </div>
                </div>

            <?php endif;?>
Share Improve this question edited Oct 12, 2019 at 13:43 Emmanuel Richman asked Oct 6, 2019 at 7:05 Emmanuel RichmanEmmanuel Richman 134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Check up the wordpress templating hierarchy The graphics reads from left to right. You have custom post type products so you can override this file and your code should be in category-products.php, since you are limiting your WP queury for products posts. Category.php is similar but without this limitation.

   // Your category-products.php loop should be like this
   <!-- Prepare query -->
   $your_query = new WP_Query( array(
     'post_type' => 'products',
     'posts_per_page' => 20,
   ));

   if ( $your_query ->have_posts() ) :

   while ( $your_query ->have_posts() ) : $your_query ->the_post();?>
   <!-- Show a card -->
   <a href="<?php the_permalink();?>">
   <div class="product">
   <div class="image-container">
   <?php the_post_thumbnail();?>
   </div> 
   <p class="title"><?php the_title(); ?></p>
   </div>    
   </a>   

   <!-- loop-end -->
   <?php endwhile;  ?>

   <!-- pagination -->
   <div class="col-md-8 post_paginations" style="text-align:center;">
   <?php the_posts_pagination( array(
   'mid_size'  => 2,
   'screen_reader_text' => __( ' ', 'theme_name' ),
   'prev_text' => __( 'Previous', 'theme_name' ),
   'next_text' => __( 'Next', 'theme_name' ),
   ) ); ?>
   </div>

   <!-- Else -->
   <?php else : ?>
   <h1><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></h1>
   <?php endif; ?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745086676a4610447.html

相关推荐

  • categories - Unable to create pagination for Category.php

    I'm trying to make a pagination for my category.php. My posts are custom post type.I get a 404 not found error whe

    17小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信