WordPress Managed Hosting - 40% Off For 4 Months - Coupon Code: BFCM2021 Avail Now

How To Use WP_Query to Create Pagination in WordPress

Moeez — June 14, 2017 3 Minutes Read

This tutorial will detail how to create a custom WordPress loop with pagination.

To implement this custom loop, I will use WP_Query class to setup a new query, and then display the posts with pagination. Now remember that the default WordPress pagination (as implemented by Next Page and Previous Page) is easy to implement and code. However, this setup does not go well with modern themes.

Custom Query

This tutorial is based on WP_Query, and thus I would recommend you read up relevant codex pages to understand how this class works.
A sample of the query being run is as follows:

Code to Render Pagination

Put the following code in a custom static page by creating a template file (with the name CusotmPage.php) in the theme folder.

Create a page in the admin area and select the template as shown below:

Pagination Function

The following code is required for the custom pagination to function. Put this code in functions.php ( located in the theme folder):

Styling for the Pagination

Open up the style.css (located in the theme folder) and add the following code:

Here is the output of the custom pagination code:

Conclusion

In this tutorial, I discussed how you could easily setup custom pagination for your WordPress website. The idea is implemented through WP_Query class. If need help with the code or have a question about this tutorial, feel free to comment below.

 

Create Faster WordPress Websites!

Free eBook on WordPress Performance right in your inbox.


    Create Faster WordPress Websites!

    Free eBook on WordPress Performance right in your inbox.

      Moeez is ‘The’ blogger in charge of WPblog. He loves to interact and learn about WordPress with people in the WordPress community. Outside his work life, Moeez spends time hanging out with his friends, playing Xbox and watching football on the weekends. You can get in touch with him at moeez[at]wpblog.com.

      THERE'S MORE TO READ

      Newsletter

        WordPress Help Zone - Ultimate WordPress Pit-Stop

        Learning WordPress? Or are you expert enough to help others? Join our WP Facebook group!