Pagination for Pages and Posts

I want to use Pagination throughout a new theme but am having trouble getting it to work correctly.

I am using some code within my functions.php file and then calling that function. It works on the ‘blog’ page but not on paginated posts or pages.

Read More

Here is my code:

// Pagination for paged posts, Page 1, Page 2, Page 3, with Next and Previous Links, No plugin
function canvas_pagination()
{
    global $wp_query;
    $big = 999999999;
    echo paginate_links(array(
        'base' => str_replace($big, '%#%', get_pagenum_link($big)),
        'format' => '?paged=%#%',
        'current' => max(1, get_query_var('paged')),
        'total' => $wp_query->max_num_pages,
        'type' => 'list'
    ));
}

add_action('init', 'canvas_pagination'); // Add our Pagination

And call the funtion: <?php get_template_part('pagination'); ?>

This code works great within the index.php file.
See here: http://website-test-lab.com/sites/blank_canvas/blog/

But not within single.php and page.php:

  • SITE URL + /paginated/
  • SITE URL + /page-pagination/
    (Can’t add more than 2x links here)

Can someone explain why this doesn’t work and what needs to be amended to fix it.

I have tried using wp_link_pages but it’s not giving me the ability to customise the output I require.

Thanks in advance

Related posts

2 comments

  1. Post Navigation

    Here’s the way its done in the Twenty Twelve themes single.php file however it looks like Twenty Fourteen has a better solution which works for pages as well.

    <nav class="nav-single">
                    <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
                    <span class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'twentytwelve' ) . '</span> %title' ); ?></span>
                    <span class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'twentytwelve' ) . '</span>' ); ?></span>
                </nav><!-- .nav-single -->
    

    You could hook this code in from your functions file using a theme specific hook or filtering the WordPress content hook.

    There’s some good examples of using the content filter in your functions file here.

    More http://codex.wordpress.org/Next_and_Previous_Links

    Page Navigation

    Here’s a tutorial i wrote which uses the code from Twenty Fourteen theme.

    It creates a template tag you can use in a custom function with hook and conditional tags or directly in a template file.

  2. Step:1 Make Function in function.php

    <?php    
    function vb_pagination( $query=null ) { 
      global $wp_query;
      $query = $query ? $query : $wp_query;
      $big = 999999999; 
    
      $paginate = paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'type' => 'array',
    'total' => $query->max_num_pages,
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'prev_text' => __('&laquo;'),
    'next_text' => __('&raquo;'),
    )
     ); 
    
     if ($query->max_num_pages > 1) :
    ?>
    <ul class="pagination">
      <?php
      foreach ( $paginate as $page ) {
        echo '<li>' . $page . '</li>';
      }
      ?>
    </ul>
    <?php
      endif;
    }
    

    Step : 2 call the pagination function require to use

    <?php if ( function_exists('vb_pagination') ) {
                        vb_pagination( $the_query );
                    } ?>
    

Comments are closed.