404 not found error on pagination

Normal post (not custom post type), I did everything I should do, but 404 not found error in pagination. Here is my code

$category_id = get_query_var('cat'); //Using this to get category ID to meet some special requirements
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

        $args = array(
            'posts_per_page' => 4,
            'numberposts'    => 50,
            'paged' => $paged,
            'cat' => $category_id
        );

        query_posts($args);
        if ( have_posts() ) : while ( have_posts() ) : the_post(); 
        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
        ?>
        <div id="post-item">
        <div class="thumb"><img src="<?php echo get_stylesheet_directory_uri(); ?>/timthumb.php?src=<?php echo $image[0]; ?>&w=224&h=224&zc=1" /></div>

        <div class="detail">
            <h2 class="wrtitle"><span class="blue"><?php  $category = get_the_category(); echo $category[0]->cat_name; ?> : </span><?php the_title(); ?></h2>
            <div class="date"><?php the_date(); ?></div>
            <div class="excerpt"><?php the_excerpt(); ?></div>
        </div>
        <div class="rmore"><a href="<?php the_permalink(); ?>"><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/readmore.png" /></a></div>
        </div>
        <?php endwhile; 
        wp_reset_postdata();
        else: ?>
     <p><?php _e('No posts found'); ?></p>

    <?php endif; ?>

    <!-- Pagination Part -->
    <div id="pagination">
        <div class="next"><?php next_posts_link('next &raquo;') ?></div>
        <div class="prev"><?php previous_posts_link('&laquo; previous') ?></div>
    </div>

I need your suggestion.

Related posts

1 comment

  1. Put your code in the template file category.php.

    Remove all the part before the loop: once in category template, you don’t need to get the category, get the paged, run again the query with query_posts

    So your category.php should simply appear like so:

    if ( have_posts() ) : while ( have_posts() ) : the_post(); 
      $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
    ?>
    
    ... post markup here
    
    <?php endwhile; 
    
    else: ?>
    
    <p><?php _e('No posts found'); ?></p>
    
    <?php endif; ?>
    
    <!-- Pagination Part -->
    <div id="pagination">
      <div class="next"><?php next_posts_link('next &raquo;') ?></div>
      <div class="prev"><?php previous_posts_link('&laquo; previous') ?></div>
    </div>
    

    To force that template to show only 4 fosts per page, in functions.php use:

    add_action('pre_get_posts','four_post_per_cat');
    
    function four_post_per_cat( $query ) {
      if ( ! is_admin() && is_main_query() && is_category() ) {
        $query->set('posts_per_page', 4);
      }
    }
    

    After that numberposts and posts_per_page are synonyms, but numberposts is deprecated. Setting different values for them make numberposts do nothing (or posts_per_page do nothing, I can’t remember… however, use one of them).

    If your scope is limiting the total posts reached (in all the pages), use the post_limit filter, in functions.php add also:

    add_filter( 'post_limits', 'cat_post_limits' );
    
    function cat_post_limits( limit ) {
        return ( is_category() ) ? 'LIMIT 0, 50' : $limit;
    }
    

    Following my tips, not also you’ll solve your issue, but also improve performace: because query_posts is very bad regarding performance: never use it.


    A note: if you have added any rewrite rule, be sure to flush rules. In you dashboard go to Settings -> Permalinks and click “Save Changes”.


    PS: If you get at max 50 posts, 50 is not divisible by 4, so last page will have 2 posts.. why don’t set limit to 52 or 48?

Comments are closed.