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.
and the URL is showing /
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(); ?>
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.
and the URL is showing http://example/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(); ?>
Share
Improve this question
edited Nov 16, 2019 at 10:03
fuxia♦
107k39 gold badges255 silver badges459 bronze badges
asked Nov 16, 2019 at 9:51
Naren VermaNaren Verma
2491 gold badge6 silver badges19 bronze badges
0
1 Answer
Reset to default 1First thing:
Category archives default to using this permalink/URL structure:
http://example/category/<category slug>/ (for page #1)
http://example/category/<category slug>/page/<page number>/ (for page #2, #3, and so on)
So when you visit http://example/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:
Change the
$tyler_query->have_posts()
tohave_posts()
.Then change the
$tyler_query->the_post()
tothe_post()
.Just use
<?php next_posts_link(); ?>
without having to pass the max number of pages (the$tyler_query->max_num_pages
in your code).