WordPress Pagination Navigation: How to Add It
- Total posts
- 50 posts
- You entered
- Posts per page
- 10 posts
- You entered
To add pagination navigation in WordPress, you can use a plugin, edit your theme’s template files, or add a code snippet. The method you choose depends on your theme and technical comfort level, but the goal is the same: replace the default “Older Posts” and “Newer Posts” links with numbered page links.
Formula
The number of pagination pages is calculated by dividing the total number of posts by the number of posts per page, then rounding up to the nearest whole number.
Pages = ceil(Total Posts / Posts per Page)
Where:
- Total Posts – the total number of blog posts or archive items (e.g., 50).
- Posts per Page – the number of posts displayed on one page (set in Settings → Reading, usually 10).
- Pages – the total number of pagination links needed.
Worked Example
Suppose you have 50 blog posts and your site shows 10 posts per page. The number of pages is:
Pages = ceil(50 / 10) = ceil(5) = 5
So you will have 5 pagination pages. The calculator above gives the same result: 5 pages.
Why Use Pagination Instead of Infinite Scroll?
Pagination divides your content into separate pages, each with a unique URL. This is different from infinite scroll, which loads more posts as the user scrolls. Pagination is often preferred because:
- It gives users control over where they are in the list.
- Each page has its own URL, which can be bookmarked or shared.
- It makes it easier for search engines to crawl and index all your content.
- It reduces the load on the server because only a subset of posts is loaded at a time.
Infinite scroll is more modern and can feel smoother, but it can hurt SEO if not implemented carefully. Pagination is the safer, more established choice for most sites.
How to Add Pagination: Plugin Method
The easiest way to add pagination is with a plugin. Here are the steps:
- In your WordPress admin, go to Plugins → Add New.
- Search for “WP-PageNavi” or “thePosts Pagination”.
- Install and activate the plugin.
- Go to Settings → PageNavi (or the plugin’s settings page) to configure the number of page numbers to show, the text for next/previous links, and styling.
- If your theme doesn’t automatically support the plugin, you may need to add a small code snippet to your theme’s
index.phporarchive.phpwhere you want the pagination to appear.
Most modern themes already support these plugins, so you can just activate and go.
How to Add Pagination: Code Method
If you prefer not to use a plugin, you can add pagination directly to your theme. The most common function is the_posts_pagination(), which is available in WordPress 4.1 and later. Here’s how to use it:
- Open your theme’s
functions.phpfile. - Add the following code to enable pagination support (if your theme doesn’t already have it):
function mytheme_pagination() { the_posts_pagination( array( 'mid_size' => 2, 'prev_text' => __( 'Previous', 'textdomain' ), 'next_text' => __( 'Next', 'textdomain' ), ) ); } add_action( 'after_setup_theme', 'mytheme_pagination' ); - Then, in your template files (like
index.php,archive.php, orsearch.php), add<?php mytheme_pagination(); ?>where you want the pagination to appear.
If your theme uses the older paginate_links() function, you can use that instead. The exact code may vary depending on your theme’s structure.
Common Problems and Solutions
Here are a few issues you might run into when adding pagination:
- Pagination not showing: This usually means your theme doesn’t have the pagination function in the right template. Double-check that you’ve added the code to the correct file (like
index.phpfor the blog page). - Pagination shows but doesn’t work: This often happens with custom queries. If you’re using
WP_Queryin a custom loop, you need to set thepagedparameter correctly. Useget_query_var( 'paged' )to get the current page number. - Page numbers are not linking correctly: Make sure you’re using
the_posts_pagination()orpaginate_links()correctly. If you’re on a static front page, you may need to usepaginate_links()with thebaseargument set toget_pagenum_link(1). - Pagination appears but is unstyled: You may need to add CSS to match your theme. Most themes have styles for
.nav-linksor.paginationclasses.
How to Choose the Right Method
Choosing between a plugin and code depends on your situation:
- Plugin: Best for beginners or if you want a quick solution with a user interface. It’s also easier to update and maintain.
- Code: Best if you want more control, don’t want to install another plugin, or if you’re comfortable editing theme files. It also avoids potential plugin conflicts.
If you’re using a page builder like Elementor or Beaver Builder, they often have built-in pagination widgets. Check your page builder’s documentation first—you might not need anything else.
Frequently Asked Questions
Why is my pagination not working on my custom query?
When you create a custom WP_Query, you need to include the paged parameter. Add 'paged' => get_query_var( 'paged' ) to your query arguments. Also, make sure you’re using wp_reset_postdata() after the loop to avoid conflicts.
Can I add pagination to a static front page?
Yes. If your front page is set to show a static page, you can still add pagination to a custom loop on that page. Use paginate_links() with the base argument set to get_pagenum_link(1) and the format argument set to 'page/%#%' to make it work.
What is the difference between the_posts_pagination() and paginate_links()?
the_posts_pagination() is a wrapper for paginate_links() that automatically uses the current query’s pagination settings. It’s simpler to use in most cases. paginate_links() is more flexible and can be used with custom queries, but requires you to set more parameters manually.
Does pagination affect SEO?
Yes. Pagination creates separate pages with unique URLs, which can be good for SEO if each page has unique content. However, you should use the rel="next" and rel="prev" link tags (or the wp_link_pages() function) to help search engines understand the relationship between pages. Most SEO plugins handle this automatically.
How do I style the pagination to match my theme?
Most themes already have CSS for the .nav-links class (used by the_posts_pagination()). If not, you can add custom CSS to your theme’s stylesheet. For example, you can style the .page-numbers class to add padding, borders, and hover effects.
Can I use pagination with a custom post type?
Yes. Pagination works with any query, including custom post types. Just make sure you set the paged parameter in your query and use the appropriate pagination function in your template.
Related
Formula used
in t | Total posts | posts | 50 | 1 in p | Posts per page | posts | 10 | 1 out pages | Total pages | pages | ceil(t / p) | 0
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.