Pagination for a Magazine Theme

Say you have a magazine-style WordPress theme. It has a slider that loads three posts, and then a list of recent posts.  The issue of pagination come up when the user hits the Older Posts link.

Maybe you have a front page that displays 3 posts in the slider. The slider is a loop. Then a default loop that shows your most recent posts. And the number of posts is determined by the setting in your Settings – Reading – Blog pages show at most X posts.

First of all, you probably don’t want to load the same template for your back pages. You don’t need the slider on your back pages, you just want a list of 10 posts.

By default, clicking the pagination link will load the same template. If you want second, and subsequent pages to skip the slider, and other front page stuff, and just show a list of posts, then you need a redirect to a different template. Here is such a function. It goes in you functions.php file. You could redirect to index.php, but then your pagination will be off by the number of posts in your slider: some posts will appears on both the front page and page 2. The fix is to duplicate index.php, and name it backpages.php. First we will put a re-direct to this template in functions.php, then we’ll fix the pagnation count on backpages.php.

<?php
function our_template_redirect() {
    if ( (is_home()) && ( is_paged('2') ) ):
        include (get_stylesheet_directory() . '/backpages.php');
        exit;
    endif;
}
add_action( 'template_redirect', 'our_template_redirect' );
?>

Next we need to fix the pagination on backpages.php. Use this code before the default loop on backpages.php, which re-calculates the offset value to account for the number of posts in the slider:

<?php         
    // We need an offset to account for the posts in the slider
    $offset = 3;

    // Next, determine how many posts per page you want, using WordPress's settings
    $ppp = get_option('posts_per_page');

    // What page are we on?
    $paged = get_query_var( 'paged' );

    // Manually determine page query offset: (offset + (current page minus one x posts per page)
    $page_offset = $offset + ( ($paged -1 ) * $ppp );

    // Define the query
    query_posts( 'offset=' . $page_offset );

    // Start the default loop
    if (have_posts()) : while (have_posts()) : the_post(); 
?>

Leave a Reply