WordPress.org

Ready to get started?Download WordPress

Forums

Pagination - Wrong category past page 1 (1 post)

  1. David Powell
    Member
    Posted 1 year ago #

    I have a categories loop that has pagination. The first page is always right. Once I get past the first page it's mixing categories together. Also, the number of pages is always wrong.

    <?php
    	global $paged;
    	$cat = get_the_category();
    	$catSlug = $cat[1]->slug;
    	$query = new WP_Query(array('category_name' => $catSlug,'posts_per_page' => 20,'paged' => $paged));
     ?>
    <?php
    	while ( $query->have_posts() ) :
    		$query->the_post();
    ?>
    		<li><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( '%s', 'starkers' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    	</ul>
    <?php
    	global $wp_query;
    
    	$catSlug = $cat[0]->slug;
    	$total_pages = $wp_query->max_num_pages;
    
    	if ($total_pages > 1){
    
      $current_page = max(1, get_query_var('paged'));
    
      echo paginate_links(array(
          'base' => get_pagenum_link(1) . '%_%',
          'format' => '?paged=%#%',
          'current' => $current_page,
          'total' => $total_pages,
          'add_args' => array('category_name' => $catSlug)
        ));
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic