WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pagination issues with category.php (5 posts)

  1. 916VT
    Member
    Posted 1 year ago #

    Hi there,

    I get some issues with my category.php template. Pagination goes to 404 error.

    I read douzen of tutorials and codex pages but I can not see where it comes from.

    Here is my code :

    <?php get_header(); ?>
    
    	<div class="cat-blog wrap space">
    
    		<section>
    
    			<h1 class="page-title">Blog</h1>
    
    			  <?php if(have_posts()) : query_posts($query_string.'&category_name=blog&posts_per_page=2'); while (have_posts()) : the_post(); ?>
    
    				<article <?php post_class(); ?> >
    					<a href="<?php the_permalink() ?>">
    					  	<?php the_title(); ?>
    					</a>
    		  		</article>
    			  <?php endwhile; ?>
    			  <?php endif; ?>
    
    				<div class="navigation">
    
    					<?php
    					  global $wp_query;
    
    					  $big = 999999999; // need an unlikely integer
    
    					  echo paginate_links( array(
    						  'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    						  'format' 		=> '?paged=%#%',
    						  'prev_next'   => false,
    						  'current' 	=> max( 1, get_query_var('paged') ),
    						  'total' 		=> $wp_query->max_num_pages
    					  ) );
    					  ?>
    				</div>
    
    				<ul id='postPagination'>
    					<li class=""><?php previous_posts_link('&laquo;&laquo;') ?></li>
    					<li class=""><?php next_posts_link('&raquo;&raquo;') ?></li>
    				</ul>
    
    		  <div class="clear"></div>
    
    		</section>
    
    	</div>
    
    <?php get_footer(); ?>

    If someone has any idea to fix it... Any help is welcome :)
    Thanks a lot

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    If this is in category.php all your category pages will only have posts from the category "blog". Is this what you want?
    http://codex.wordpress.org/Pagination#Troubleshooting_Broken_Pagination

  3. 916VT
    Member
    Posted 1 year ago #

    Hi keesiemeijer,

    Yes, I would like to have all 'blog' posts but, displayed by 5 per page (I set 2 only for trying pagination).

    BTW - I now set this template as category-blog.php but it's still not working...

  4. 916VT
    Member
    Posted 1 year ago #

    I'm trying the different points from the link you sent but that's a bit too much of php for me...

  5. 916VT
    Member
    Posted 1 year ago #

    Hi there,

    Finally, I added that part of code, from the link you sent, and adapted it and it works :

    function my_post_queries( $query ) {
      // do not alter the query on wp-admin pages and only alter it if it's the main query
      if (!is_admin() && $query->is_main_query()){
        // alter the query for the home and category pages
        if(is_home()){
          $query->set('posts_per_page', 3);
        }
        if(is_category()){
          $query->set('posts_per_page', 3);
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    I don't know why but it works...

    Thanks a lot for helping !

Topic Closed

This topic has been closed to new replies.

About this Topic