0

I'm using this code to display page pagination on my website. But when I click to go to the second page it shows me "Page not found" error.

Does someone know what is wrong with my code that is not displaying the posts on the second page?

<?php
$currCat = get_category(get_query_var('cat'));
$cat_name = $currCat->name;
$cat_id   = get_cat_ID( $cat_name );
?>

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query- 
>query('showposts=3&post_type=post&paged='.$paged.'&cat='.$cat_id);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>


<div class="module-container">;
<div class="content">;

<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); 
?></a>
</div>
</div>

<?php endwhile; ?>

<?php
global $wp_query;

 $big = 999999999; // need an unlikely integer
 echo '<div class="paginate-links">';
 echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) 
) ),
'format' => '?paged=%#%',
'prev_text' => __('<<'),
'next_text' => __('>>'),
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
echo '</div>';
 ?>
5
  • 1
    A 404 is determined by the results of the main query, not the query you create in the template. Alter the main query with pre_get_posts rather than create a new query.
    – Milo
    Commented Mar 26, 2018 at 1:25
  • I fixed the problem. I had to change the amount of post per page in my WordPress dashboard > Settings > Reading > Blog pages show at most. Commented Mar 26, 2018 at 1:28
  • That doesn't really fix the problem, it just hides it. You'll have the same issue again when the number of pages in your custom query exceeds the number of pages in the main query.
    – Milo
    Commented Mar 26, 2018 at 1:30
  • Can you show me how do I add the action in my main query? Commented Mar 26, 2018 at 1:53
  • Have you read this? codex.wordpress.org/…
    – Ravi
    Commented May 7, 2019 at 12:17

1 Answer 1

-2

Just set the permalink to numeric ( Under Setting -> Permalinks ). This works for me.

1
  • Please edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Commented May 20, 2020 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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