I'm using the bones wordpress theme, which has the following page navi function:
// Numeric Page Navi (built into the theme by default)
function bones_page_navi($before = '', $after = '', $myquery = null) {
global $wp_query;
$bignum = 999999999;
$maxpages = $myquery ? $myquery->max_num_pages : $wp_query->max_num_pages;
if ( $maxpages <= 1 )
return;
echo $before;
echo '<nav class="pagination cf">';
echo paginate_links( array(
'base' => str_replace( $bignum, '%#%', esc_url( get_pagenum_link($bignum) ) ),
'format' => '',
'current' => max( 1, get_query_var('paged') ),
'total' => $maxpages,
'prev_text' => '<',
'next_text' => '>',
'type' => 'list',
'end_size' => 2,
'mid_size' => 1,
'add_args' => false
) );
echo '</nav>';
echo $after;
} /* end page navi */
I have the following action for setting the number of posts that show for the blog archives. It's causing the page navi to return the wrong results sometimes. Here's an example: http://goo.gl/x9Krjp
Here's the action function:
/* =Posts per page for blog
-------------------------------------------------------------- */
function my_post_queries( $query ) {
// not an admin page and it is the main query
if (!is_admin() && $query->is_main_query()){
if( is_home() || is_archive() ){
$first_page_total= 7; // total number of posts on first page
$paginated_total = 8; // total number of posts on paginated pages
$posts_to_skip = $paginated_total - $first_page_total;
// pagination for custom page(s)
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
// first page query args
$query->set('posts_per_page', $first_page_total);
if(is_paged()) {
// add paginated query args
$offset = (($paged - 1) * $paginated_total)- $posts_to_skip;
$query->set('offset', $offset);
$query->set('posts_per_page', $paginated_total);
}
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );
If someone could point me in the right direction in terms of why that pagination won't work properly I'd appreciate the help.
Thanks! J.
posts_per_page
, which in turn changes the number of result pages - the bones function only works off the current request, it has no idea that depending on the current page, the number of pages will change.max_num_pages
is slightly stuffed up. You need to recalculatefound_posts
in order forWP_Query
to calculate the correct amount of pages. See this post