I've ran into a problem where I can't seem to get pagination to work on the latest wordpress version 3.4.2. I have developed a lot of wordpress sites so this rather confusing. I've got a custom post type of news, I wish to show 8 posts per page and the page url would be '/news'. This page displays correctly with the right pagination at the bottom (using WP-pagenavi). When I click on the second page (/news/page/2) I get my 404 page. I'd really appreciate any help on resolving this.
<?php
/*
Template Name: News
*/
?>
<?php get_header(); ?>
<div class="container_12">
<div class="grid_12 box">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'news', 'posts_per_page' => 8, 'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="news">
<h4><?php the_title() ?></h4></a>
<span><?php the_time('F jS, Y'); ?></span>
<p><?php limit_excerpt("30"); ?></p>
</div>
<?php endwhile; ?>
<div id="pagination">
<?php if(function_exists('wp_pagenavi')) {
wp_pagenavi( array(
'query' =>$loop
));
}
?>
</div>
</div>
</div><!-- .container_12 -->
<?php get_footer(); ?>
[Update - Solution]
So I've coded a solution for this. Put this in the functions.php
//Set for cpts
function cpt_init( $cpt )
{
add_rewrite_rule(
"{$cpt}/page/([^/]+)/?",
"index.php?pagename={$cpt}&pag=$matches[1]",
'top' );
}
//Only Add Once
add_filter( 'query_vars', 'cpt_query_vars' );
function cpt_query_vars( $query_vars )
{
$query_vars[] = 'pag';
return $query_vars;
}
//Add for each type
add_action( 'init', 'cpt_init', 'news' );
And in your loop use get_query_var('pag')
instead of get_query_var('page')