How to Add WordPress Pagination

5 minutes read

Tutorials

One of our readers asked us recently How to Add WordPress Pagination.

By inserting old posts and new posts links at the bottom of the archive pages of your WordPress you display pagination links. However, numerical pagination is used by many WordPress pages. Our experience shows that numerical pagination is easy to use, more appealing , easy and SEO Friendly.

In this article, we will teach you some of the easiest ways to enable WordPress Pagination and precisely the numeric pagination.

What is Pagination?

Pagination is a feature common to many blogs in WordPress. It is very important because it is a big feature in the WordPress center for so many site owners .. Pagination is an simple and standard practice which stops you from loading all your items simultaneously.  Many topics have also been developed into pagination.

However, if they do not, you can conveniently configure paginations with a few lines of code on your website. In this article, we will explore a couple of different methods of handling pagination on your site.

How to Manually Add WordPress Pagination?

You can use built-in WordPress functions and a few lines of codes if you create your own theme or want to add pagination to your theme. In fact, there are two different insertion methods.

With “Previous Posts” and “Next Posts” or ( << Prev 1 … 3 4 5 6 7 … 9 Next >> ) are attached to the bottom of the page.

Method 1: Attaching Previous and Next Post Links

To add pagination support to your theme, most code should be included in your theme’s functions.php file or children’s subject. I would recommend the creation of a children’s issue with custom features when using a premium theme.

For adding pagination to a WordPress theme, we need to create the feature that gives you links before and after at the bottom of the list.

How to Add WordPress Pagination

That being said, we must also ensure that certain connections are only shown when posts are specifically connected to. The first step is to build the function effectively. You can do this by opening your functions.php document, scrolling down, and inserting this code:

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 }
}

This feature would then produce a previous and next connection, wrapped in an HTML5 navigation tag, when applied to a template. The first component of the feature verifies that more than 1 page of posts are currently available. If not, the code is not needed.

After that, the HTML markup is organized. We start with a fundamental nav tag and then use the functions next posts link and previous posts link within div tags. Both these functions support the first argument for HTML, which can be used for customizing the output text. In each (<-New Post) we have a bit Javascript and a Javascript arrow to style with CSS later.

You just have to apply this functionality to the real template once you have added it. For this reason, go to the template where the index.php file, normally, is paged and scroll down to the end of the loop. This follows the sentence “endwhile” but some statements “then” inside the loop. Add this one line, then:

<?php pagination_nav(); ?>

This will display links to your page below your content and will only display links if necessary.

Method 2: Attaching Page Number Pagination

How to Add WordPress Pagination

The setting up is somewhat close to how we configure the fundamental pagination, but rather than the following and previous postal links we will use the WordPress paginate links feature. This will show a total paging bar anywhere on your models. The first bit of code in our theme or child theme functions.php file can again be placed. We can use paginated links to measure the number of pages available and present a list of links.

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,
        ));
    }
}

Again, a $wp query global object is added to allow us to view all our articles. Then, by using the max num pages feature, we calculate the number of pages available on our page and perform conditional checks to ensure there are more than 1 page, requiring pagination. None would be shown otherwise. Finally, to search the page we’re in now, we use get_query_var.

The final move is to use the paginate links feature to make the pages list and the previous as well as next buttons.

Please also go to the website (typically index.php) and paste the following code just before the end of our loop, after “endif” but before “endwhile.”

<nav class="pagination">
<?php pagination_bar(); ?>
</nav>

You can now see the complete list of connected websites, as seen in the picture above rather than the basic next or previous links.

Add WordPress Pagination Using Plugins

If you are not into manual addition of WordPress Pagination, you can also use some WordPress Plugins to add these Pagination easily. Here are some plugins you can use:

WP-PageNavi

How to Add WordPress Pagination

There are a few plugins that may help if you are looking for a bit of custom pagination or you don’t want to manually execute it. Maybe WP-PageNavi is the most common because it has to be conveniently set up, customizable, and fashionable. We described, themes from Elegant themes are set up to automatically start using WP-PageNavi with built-in PHP features and CSS styles, so you just have to trigger the plugin.

To add pagination to your page using this Plugin, you need to add the following function:

<?php wp_pagenavi(); ?>

You can then use the Settings page of the plugin to personalize every aspect of the pagination. Select the text to display, when paging, and the format of each page area for the next and previous links.

WP-Paginate

How to Add WordPress Pagination

WP Paginate is pretty much like WP-Page Navi, but you might find some additional functions useful. For example , if you wish to customize later with CSS or choose the exact page number to show on your pagination, then you can control the mark-up in which your pagination is wraps up, so that it does not get too unworkable. Such as WP-PageNavi, a single function adds page numbers to your page, replacing your existing pagination.

How to Add WordPress Pagination
WP-Paginate Customization
<?php if(function_exists('wp_paginate_comments')) {
    wp_paginate_comments();
} ?>

Your theme would have a comprehensive list of pages that can be changed with the settings of the plugin. If you are finding a dead easy solution that is not trapped with so many options, WP-Paginate works well and works only automated.

Sum Up

Congratulations, you have finally learned How to Add WordPress Pagination. Moreover, some of the visitors also asked us about How to Fix the Updating Failed WordPress. Do check the dedicated article on this.

Contact our Help Staff if you need assistance with WordPress Pagination errors. With any WordPress problems, our support team will help you.

We accept any type of suggestions from the visitors because it always motivates us to improve. Feel free to comment below.

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Related Articles

Menu