0

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.

2
  • Simply put: you're dynamically changing 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. Feb 10, 2015 at 19:00
  • It will behave strange because with the current setup, the calculation of max_num_pages is slightly stuffed up. You need to recalculate found_posts in order for WP_Query to calculate the correct amount of pages. See this post Feb 10, 2015 at 19:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.