Post Pagination - ar-to/WordPress-Theme GitHub Wiki

Refer to [Resources] for links and website about pagination.There are a couple of ways to add pagination. It can be added into a theme functions.php, as a plugin, or child theme. Below I show some ways to do this.

1.next_posts_link() & previous_posts_link

Add

      <p><?php next_posts_link( '&laquo; Previous' ); ?></p>
      <p><?php previous_posts_link( 'Next &raquo;' ); ?></p>

before or after while() loop inside index.php or other template. This method still shows a previous link on the last post and next link on the first. It can also be placed inside <nav> tags.

<?php if ( have_posts() ) :?> 

<p><?php next_posts_link( ‘Previous’ ); ?></p>

<?php while ( have_posts() ) : the_post(); ?>
          <!--Markup... Display post content-->
      <?php endwhile; ?>
      <nav>
      <ul class="pager">
      <li><?php next_posts_link( 'Previous' ); ?></li>
      <li><?php previous_posts_link( 'Next' ); ?></li>
      </ul>
      </nav>
      <?php endif; ?>

2.Create a function

This method does not show if there are no more than 1 post. This can be change in Settings>General. Add the following function below //Add additional functions here or anywhere towards the bottom of functions.php

function pagination_nav() {
    global $wp_query;

    if ( $wp_query->max_num_pages > 1 ) { ?>
        <nav class="pagination" role="navigation">
            <div class="nav-previous"><?php next_posts_link( '&larr; Older posts' ); ?></div>
            <div class="nav-next"><?php previous_posts_link( 'Newer posts &rarr;' ); ?></div>
        </nav>
<?php }
}

Then add this code to index.php or wherever the pagination is wanted

<?php pagination_nav(); ?>

3.Create function with page numbers

This method also does not show pagination when only one post. Add this to functions.php

function pagination_bar() {
    global $wp_query;

    $total_pages = $wp_query->max_num_pages;

    if ($total_pages > 1){
        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
        ));
    }
}

Then add this code to index.php or other template

<nav class="pagination">
<?php pagination_bar(); ?>
</nav>
⚠️ **GitHub.com Fallback** ⚠️