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 »') ?></div>
<div class="prev"><?php previous_posts_link('« previous') ?></div>
</div>
I need your suggestion.
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:To force that template to show only 4 fosts per page, in
functions.php
use:After that
numberposts
andposts_per_page
are synonyms, butnumberposts
is deprecated. Setting different values for them makenumberposts
do nothing (orposts_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, infunctions.php
add also: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?