Pagination

MDB Pagination

Author: Dawid Adach

-

Pagination

It seems that we are ready to start blogging using our brand new theme. However, probably after adding a few posts, you would notice that we have a small bug. By default, WP displays ten posts in the main loop. Once we create 11th one, a very first post will disappear from the main page and won't be accessible anymore. If you haven't done that before, add at least five posts to your blog, and change number of displayed post. In order to do that go to Settings > Reading and set Blog pages to show at most to 3. Now you can see issue yourself.

Reading settings

In some cases, it's fine to show only 3 latest posts (i.e. when you build a landing page for product). If that is the case you can simply remove a pagination code from your index.php file and your theme will always display only the 3 newest posts.

However, if you want let you user read previous posts, follow these steps.

  1. Create a new folder called inc, and add a new file pagination.inc.php inside. Paste following code into the file:
  2. <?php
    //Custom pagination
    function mdb_pagination() {
    if( is_singular() )
    return;
    global $wp_query;
    /** Stop execution if there's only 1 page */
    if( $wp_query->max_num_pages <= 1 )
    return;
    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $max   = intval( $wp_query->max_num_pages );
    /** Add current page to the array */
    if ( $paged >= 1 )
    $links[] = $paged;
    /** Add the pages around the current page to the array */
    if ( $paged >= 3 ) {
    $links[] = $paged - 1;
    $links[] = $paged - 2;
    }
    if ( ( $paged + 2 ) <= $max ) {
    $links[] = $paged + 2;
    $links[] = $paged + 1;
    }
    echo '<nav id="mdb-navigation" class="d-flex justify-content-center my-4 wow fadeIn">' . "\n";
      echo '<ul class="pagination pagination-circle pg-info mb-0">' . "\n";
        /** Previous Post Link */
        if ( get_previous_posts_link() )
        printf( ' <li>%s</li>
        ' . "\n", get_previous_posts_link('
        <span aria-hidden="true">&laquo;</span>
        <span class="sr-only">Previous</span>
        ') );
        /** Link to first page, plus ellipses if necessary */
        if ( ! in_array( 1, $links ) ) {
        $class = 1 == $paged ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
        if ( ! in_array( 2, $links ) )
        echo '<li>…</li>';
        }
        /** Link to current page, plus 2 pages in either direction if necessary */
        sort( $links );
        foreach ( (array) $links as $link ) {
        $class = $paged == $link ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
        }
        /** Link to last page, plus ellipses if necessary */
        if ( ! in_array( $max, $links ) ) {
        if ( ! in_array( $max - 1, $links ) )
        echo '<li>…</li>' . "\n";
        $class = $paged == $max ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
        }
        /** Next Post Link */
        if ( get_next_posts_link() )
        printf( '<li>%s</li>
        ' . "\n", get_next_posts_link('<span aria-hidden="true">&raquo;</span>
        <span class="sr-only">Next</span>') );
      echo '</ul>' . "\n";
    echo '</nav>' . "\n";
    echo '<!--/.Pagination-->' . "\n";
    }
    ?>
        
  3. Open functions.php file and add following lines at the beginning (after <?php)
  4. /**
     * Include external files
     */
    require_once('inc/pagination.inc.php');
    
    
  5. Within the index.php file, remove pagination snippet:
  6. <!--Pagination -->
                    <nav class="d-flex justify-content-center my-4 wow fadeIn">
                        <ul class="pagination pagination-circle pg-info mb-0">
    
                            <!--First-->
                            <li class="page-item disabled">
                                <a class="page-link">First</a>
                            </li>
    
                            <!--Arrow left-->
                            <li class="page-item disabled">
                                <a class="page-link" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                    <span class="sr-only">Previous</span>
                                </a>
                            </li>
    
                            <!--Numbers-->
                            <li class="page-item active">
                                <a class="page-link">1</a>
                            </li>
                            <li class="page-item">
                                <a class="page-link">2</a>
                            </li>
                            <li class="page-item">
                                <a class="page-link">3</a>
                            </li>
                            <li class="page-item">
                                <a class="page-link">4</a>
                            </li>
                            <li class="page-item">
                                <a class="page-link">5</a>
                            </li>
    
                            <!--Arrow right-->
                            <li class="page-item">
                                <a class="page-link" aria-label="Next">
                                    <span aria-hidden="true">&raquo;</span>
                                    <span class="sr-only">Next</span>
                                </a>
                            </li>
    
                            <!--Last-->
                            <li class="page-item">
                                <a class="page-link">Last</a>
                            </li>
    
                        </ul>
                    </nav>
    
  7. With a short function:
  8. <?php mdb_pagination(); ?>
    
  9. Update footer.php file and add following lines after </body>:
  10. <script>
    $("#mdb-navigation > ul > li").addClass("page-item")
    $("#mdb-navigation > ul > li > a").addClass("page-link")
    </script>
    

Our pagination is ready now. It automatically creates numerous pages depends on a number of posts and configuration (you can play with it by adding more posts and decreasing number of displayed posts per page). It also highlights current page which we are browsing.

Pagination example

Our pagination function is quite complex, and we will not explain it in detail. What you should remember is that WP comes with default pagination mechanism, but it also allows to overwrite it with the custom one in order to give it a personalized design. By now you should also know that you are free to create custom functions and calling them within your theme.


Previous lesson Download Live preview Next lesson

Spread the word:
Do you need help?: Use our support forum

About the author

Dawid Adach
For more than 5 years Dawid worked as an IT Consultant specializing in SOA/EAI/ESB in the banking domain. He gained experience working in countries like Netherlands, Belgium, Poland and India developing enterprise-class systems for the most prestigious companies. Since co-founding mdbootstrap.com & brandflow.net in 2016 he has been using and teaching technologies such as Angular, TypeScript, PHP, AJAX, Mongo, SQL, Hadoop Stack, Virtualization, Automation and many others...