WordPress Hosting Coupon Code | 3 Months | 100% OFF Get Offer

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:

Get Blazing Fast WordPress Hosting With
$25 FREE CREDIT

Use Promo Code: WPB25

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.

 

Subscribe to Get a FREE WordPress Ebook Right in Your Inbox

WPblog provides the complete guide to launch your WordPress website completely FREE!

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

WP HelpZone - Ultimate WordPress Pit-Stop

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

Also be a part of Giveaways & Discounts exclusively for YOU!