pagination error in category page wordpress

i Have the Same problem in category page

the problem i have pagination works fine in all site and all works fine but when it came to category page and i navigate to %category_name%/page/2 it gives me 404 error

Read More

i Use postname structure permalink from Settings > permalink > postname

i tried alot to solve this problem but i didnot find anything useful

what i dont understand when i use the url like this

Heading

%category_name%/?page=2

it works fine i dont know how to fix this please anyone responed

This is my Code

<?php get_header(); ?>
<div class='container'>
  <?php the_breadcrumb(); ?>
  <!-- *Captionbox*/ -->
  <?php
        echo "<div class='captionbox style2'>";
            echo '<h1>'.single_cat_title( '', false ).'</h1>';
            echo '<p>'.category_description( ).'</p>';
        echo "</div>";
  ?>
</div>
<div class='container no-padding'>
<?php
    /**
     * The WordPress Query class.
     * @link http://codex.wordpress.org/Function_Reference/WP_Query
     *
     */
    $args = array(
        //Category Parameters
        'category_name'    => single_cat_title( '', false ),
        //Type & Status Parameters
        'post_type'   => 'product_post',
        //Order & Orderby Parameters
        // 'order'               => 'DESC',
        // 'orderby'             => 'date',
        //Pagination Parameters
        // 'posts_per_page'         => 2,
        'paged'                  => get_query_var('page'),
    );

    query_posts( $args );

    if( have_posts() ) :
    echo "<ul class='categoryContainer'>";
            while ( have_posts() ) 
            {
                the_post();
                $id = get_the_id();
                $post_url = get_permalink( $id );

                $categories = wp_get_post_terms( $id, 'category' );
                $brands = wp_get_post_terms( $id, 'brand_taxonomy' );
                $brand_url = get_term_link( $brands[0]->term_id, 'brand_taxonomy' );
                $brand_img_url = z_taxonomy_image_url($brands[0]->term_id);

                $slugs = array();

                foreach ($categories as $category) :
                    $slugs[] = $category->slug;
                endforeach;

                $slug = implode(' ', $slugs);

                $img_url    = wp_get_attachment_url( get_post_thumbnail_id($id) );
                echo "<li class='col-lg-3 col-md-4 col-sm-6 col-xs-12'>";
                  echo "<div class='card'>";
                    echo "<span class='brand'><a href='".$brand_url."'>";
                      echo "<img src='".$brand_img_url."'>";
                    echo "</a></span>";
                        echo "<img alt='image' src='".$img_url."'>";
                    echo "<a href='".$post_url."'>";
                        echo "<h6>".get_the_title()."</h6>";
                        echo "<p>".get_the_excerpt()."</p>";
                    echo "</a>";
                    echo "<a class='btn' href='".$post_url."'>Details</a>";
                  echo "</div>";
                echo "</li>";
            }
    echo "</ul>";
    echo "<div class='clearfix'></div>";
    next_posts_link( 'Older posts' );
    previous_posts_link( 'Newer posts' );
    endif;
    wp_reset_query();
?>
</div>
<?php get_footer(); ?>

Related posts

Leave a Reply

1 comment

  1. Same thing I been struggling with but I was actually trying to display a category posts from multiple custom post types. i got it working for one custom post type and by reading your question seems like that’s what you need. Have a look at my code, it might help you…

                                  <div class="col-md-9 exerpts-wrap">
              <div class="row">
    
                <?php if (have_posts()) : ?>
    
                <?php if ( is_category('uncategorized')) {  ?>     
    
                  <?php
                  $query_string = array(
                  'post_type' => array( 'post', 'videos', 'resources', 'demos' ));
    
                   query_posts($query_string);  ?>
    
                  <?php while (have_posts()) : the_post(); ?>
    
                  <div class="col-sm-6">
                    <div class="blog-sec">
    
                    <?php get_template_part('content', get_post_format()); ?>
    
                    </div> <!-- blog-sec ends here -->
    
                  <?php endwhile; ?>
    
                  <?php } ?>     
    
                  <?php endif; ?>
    
                </div> <!-- col-sm-6 ends here -->
    
              <?php pagination_numeric_posts_nav(); ?>
              </div> <!-- exerpts-wrap fix ends here -->
    

    Mine is custom pagination function but you can just use yours and it should work.