1

I have a category page and I am getting some issues on pagination. When I click on NEXT PAGE then I am getting

Oops! That page can’t be found.

enter image description here

and the URL is showing http://example.com/category/accessories/page/2/ which is correct.

Below is my full code which is I am using on the category page.

<?php
   /**
   * A Simple Category Template
   */
get_header(); 
?>  
<div id="primary" class="content-area">
   <main id="main" class="site-main">
      <div class="container">
         <div class="equalPadding"> 
   <?php 
  global $paged;
  $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
  $args = array('posts_per_page' => 6,'paged'=> $paged,); 
  $tyler_query = new WP_Query( $args );
       if ($tyler_query->have_posts()){
          if(is_category()){          
                 $category_images = get_option( 'category_images' );
                 $category_image = '';
                 if ( is_array( $category_images ) && array_key_exists( get_query_var('cat'), $category_images ) ){
                 $category_image = $category_images[get_query_var('cat')] ;
                 ?>        
            <div class="category-img" style="background-image:url(<?php echo $category_image;?>)">
            <div class="category_heading d-flex related-post">
            <div class="cp-heading"><h1><?php single_cat_title(''); ?></h1></div>
        <?php   //check if category is a subcategory

    if( 0 != $this_category->parent ){ //if subcategory then display feature post
         //displaying feature post here
    $args = array('posts_per_page' => 3,'meta_key' => '_featured-post','meta_value' => 1);
    $featured = new WP_Query($args);
    if ($featured->have_posts()):?><div class="subCat_featurePost post-list ">
            <?php while($featured->have_posts()): $featured->the_post();?>
            <div class="item"> <div class="thumb"><a href="<?php esc_url(the_permalink()); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail();?></a></div>
             <div class="title"><a href="<?php echo esc_url( get_the_permalink() );?>" title="<?php echo esc_attr( the_title_attribute('echo=0'));?>"><?php echo wp_trim_words(get_the_title(), 16, '...');?></a></div></div>
              <?php endwhile;?></div><?php else:
    endif;
    }?>
    </div>
  </div>
  <?php } } ?>
      <div class="categoryWrapper_list cp-seeWrapper">
            <div class="row">
       <?php $index = 0;
            $check=0;
//Main category list displaying from here
        while ($tyler_query->have_posts()){
           $tyler_query->the_post();
     if ($index < 4) {?>
        <div class="col-xl-12 col-lg-2 col-md-12 col-sm-12 col-xs-12">  
        <div class="row spaceBottom cp-shadow bg-white cp-homeRelated">
        <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12 padding0">
        <div class="cp-homeRelatedImg"><a href="<?php esc_url(the_permalink()); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail();?></a>
       </div>
          </div>
    <div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-xs-12 colcentertext">
        <div class="marginautoWrapper">
        <?php $category = get_queried_object();
              if ( ! empty( $category ) ) {
              $cat_list='<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . $category->name . '</a>';
               }?>
        <div class="d-flex cp-CategoryList">
                <div class="seeDate"><?php echo get_the_date('F j, Y');?></div>
                <div class="cp_cat_list"><?php echo $cat_list;?></div>
              </div>
          <div class="cp-b-content">
            <h2><a href="<?php echo esc_url( get_the_permalink() );?>" title="<?php echo esc_attr( the_title_attribute('echo=0'));?>"><?php echo wp_trim_words(get_the_title(), 16, '...');?></a></h2>
<p><?php echo wp_trim_words(get_the_excerpt(), 35, '...');?></p>
            <h5>By <span><?php the_author(); ?></span></h5>
    </div>
    </div>
  </div>  
  </div>
    </div>
      <?php  } else {
        if ($check==0){?>
         <div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-xs-12 padding0"><div class="row">
      <?php $check=1;}?>
        <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12">
          <div class="cp-shadow cp-seeSinglePostWrapper">
            <a href="<?php echo esc_url( get_the_permalink() );?>" title="<?php echo esc_attr( the_title_attribute('echo=0'));?>" class=""><?php echo get_the_post_thumbnail();?></a>
            <div class="bg-white single-post-box">
              <div class="d-flex cp-CategoryList">
                <div class="seeDate"><?php echo get_the_date('F j, Y');?></div>
                <div class="cp_cat_list"><?php echo $cat_list;?></div>

              </div>
              <div class="cp-b-content">
                <h2><a href="<?php echo esc_url( get_the_permalink() );?>" title="<?php echo esc_attr( the_title_attribute('echo=0'));?>" class=""><?php echo wp_trim_words(get_the_title(), 12, '...');?></a>
                </h2>
                  <p><?php echo wp_trim_words(get_the_excerpt(), 25, '...');?></p>
              <h5>By <span><?php the_author(); ?></span></h5>
                </div>
            </div>
            </div>
        </div>
           <?php   } $index++;}?></div></div>

  <?php if ($index > 4) {?>
  <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12">
        <div class="cp-social cp-shadow bg-white">
        <h3>Social</h3>
           <?php if (is_active_sidebar('footer-social')) :
                 dynamic_sidebar('footer-social');
                endif;
      ?>
  </div>
    <div class="cp-newsletter cp-shadow bg-white">
         <?php  if (is_active_sidebar('cp_newsletter')) :
                 dynamic_sidebar('cp_newsletter');
                endif;?>
    </div>
</div>
       <?php } ?>
  <div class="cp-postPagination">
     <div class="nav-previous cp-pagi float-left"><?php previous_posts_link(); ?></div>
       <div class="nav-next cp-pagi float-right"><?php next_posts_link( null, $tyler_query->max_num_pages ); ?> </div>
      <div class="clearfix"></div>
  </div>

  </div>
</div>
    <?php } else{?><p>Sorry, no posts matched your criteria.</p><?php } ?>


         </div>
      </div>
   </main>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
1

First thing:

Category archives default to using this permalink/URL structure:

http://example.com/category/<category slug>/ (for page #1)
http://example.com/category/<category slug>/page/<page number>/ (for page #2, #3, and so on)

So when you visit http://example.com/category/accessories/page/2/, WordPress will automatically make a call to WP_Query and the parameters being used are retrieved from the current page URL:

WP_Query( array(
  'category_name' => 'accessories', // the <category slug> in the URL
  'paged'         => 2,             // the <page number> in the URL
) );

And that is the main query.

So when the paged value is greater than the max number of pages for the main query, then a 404 error page would be displayed. And in your case, does the category accessories actually have enough number of posts for the archive page to have a page #2? I bet no.

Second thing:

Category templates should just display posts retrieved via the main query:

while ( have_posts() ) {
    the_post();
    // Display the post.
}

But I'm not seeing that anywhere in your code.

Third thing:

You can use pre_get_posts to alter the main query by modifying its parameters:

// This should go in your theme's functions.php file.
add_action( 'pre_get_posts', function ( $query ) {
    // Set the number of posts per page to 6, but only on category archives.
    if ( ! $query->is_admin && $query->is_main_query() && is_category() ) {
        $query->set( 'posts_per_page', 6 );
    }
} );

Then in your code, there's no necessity for this:

$args = array('posts_per_page' => 6,'paged'=> $paged,);
$tyler_query = new WP_Query( $args ); // secondary query

Additionally:

  1. Change the $tyler_query->have_posts() to have_posts().

  2. Then change the $tyler_query->the_post() to the_post().

  3. Just use <?php next_posts_link(); ?> without having to pass the max number of pages (the $tyler_query->max_num_pages in your code).

  • Yes, I have while ( have_posts() ) { in my answer but I am using like while ($tyler_query->have_posts()){ in my answer. – Naren Verma Nov 17 '19 at 7:19
  • Check the updated answer and do let me know. – Sally CJ Nov 17 '19 at 9:12
  • 1
    Sorry for the late reply, I checked your previous answer, and it was working. I added some code (which you shared pre_get_posts)in function.php and remove the WP_Query code and change the next_posts_link. – Naren Verma Nov 17 '19 at 11:41
  • In that case, then you can accept that answer and/or upvote it.. :) – Sally CJ Nov 17 '19 at 12:02
  • 1
    Yes, It's working...upvote from my side. – Naren Verma Nov 17 '19 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.