To paginate your WordPress site without plugins, you can manually insert pagination into your theme’s code. This involves editing your theme files, so it’s a good idea to back up your site before making any changes. Here’s a basic approach to manually add pagination to your WordPress site:
Step 1: Edit Your Theme’s functions.php
File
- Enable WordPress Numeric Pagination: Add the following function to your theme’s
functions.php
file. This function generates the numeric pagination.
function numeric_posts_nav() { 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 class="pagination"><ul>' . "\n"; /** Previous Post Link */ if ( get_previous_posts_link() ) printf( '<li>%s</li>' . "\n", get_previous_posts_link() ); /** 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() ); echo '</ul></nav>' . "\n"; }
Step 2: Call the Pagination Function in Your Theme Files
- Implement Pagination in Theme Templates: Where you want the pagination to appear (e.g., in your archive, index, or category templates), call the
numeric_posts_nav()
function. You can do this by adding the following line of code:
<?php numeric_posts_nav(); ?>
This code snippet will add a numeric pagination to your WordPress site without using any plugins. Remember, any changes made to the theme files will be overwritten when the theme is updated. To prevent this, consider creating a child theme or using a custom site-specific plugin to add custom functions.
For those that want to learn more: Line by line explanation of the how to paginate your WordPress site without plugins:
Let’s break down the PHP function numeric_posts_nav()
line by line to understand how it adds numeric pagination to a WordPress theme without plugins:
Function Definition
function numeric_posts_nav() {
This line defines a new function named numeric_posts_nav
. Functions in PHP are blocks of code that can be executed repeatedly from anywhere in the code.
Check for Singular
if( is_singular() ) return;
is_singular()
is a conditional tag in WordPress that checks if a single post is being displayed. If true, the function returns early, as pagination isn’t needed for singular views.
Global Variable
global $wp_query;
This line makes the global variable $wp_query
accessible inside the function. $wp_query
contains the WordPress Query object for the current request.
Early Return for Single Page
if( $wp_query->max_num_pages <= 1 ) return;
Checks if there’s only one page of posts to display. If true, there’s no need for pagination, so the function returns early.
Page Number Variables
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1; $max = intval( $wp_query->max_num_pages );
$paged
stores the current page number. $max
stores the total number of pages.
Arrays for Page Links
if ( $paged >= 1 ) $links[] = $paged; if ( $paged >= 3 ) { $links[] = $paged - 1; $links[] = $paged - 2; } if ( ( $paged + 2 ) <= $max ) { $links[] = $paged + 2; $links[] = $paged + 1; }
These lines prepare an array $links
that will store the page numbers to be displayed as links.
Beginning of Navigation Markup
echo '<nav class="pagination"><ul>' . "\n";
Outputs the beginning of the navigation HTML markup.
Previous Page Link
if ( get_previous_posts_link() ) printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
Checks if there’s a previous page link available and prints it.
Generating Page Number Links
if ( ! in_array( 1, $links ) ) { ... } sort( $links ); foreach ( (array) $links as $link ) { ... }
This segment ensures the pagination starts with “1”, adds an ellipsis if necessary, sorts the $links
array, and then iterates over each link.
Last Page and Ellipsis Logic
if ( ! in_array( $max, $links ) ) { ... }
Checks if the last page number is not in the $links
array and adds an ellipsis if necessary.
Next Page Link
if ( get_next_posts_link() ) printf( '<li>%s</li>' . "\n", get_next_posts_link() );
Similar to the previous page link, this checks for a next page link and prints it if available.
Closing HTML Tags
echo '</ul></nav>' . "\n"; }
Closes the unordered list (<ul>
) and the navigation (<nav>
) HTML tags, marking the end of the pagination markup.
Code output for pagination. The demonstrated code examples should run similar to this: The pagination has been dummied for illustration purposes.
WordPress Pagination: How to Add Pagination Manually or With Plugins : Videos
The lesser plugins the better. Having numerous WordPress Plugins on your site is detrimental for several reasons, and here’s why:
- Performance Issues: Each plugin adds some load to your website. More plugins mean more code for the server to execute, which can significantly slow down your website’s loading time. This slow performance can negatively affect user experience and SEO rankings.
- Security Risks: Plugins can introduce security vulnerabilities. Not all plugins are developed with strict security standards, and keeping many plugins increases the risk of your site being compromised. Moreover, the more plugins you have, the higher the likelihood that some of them might not be regularly updated, leaving your site exposed to known vulnerabilities.
- Compatibility Issues: With each plugin, there’s a risk of compatibility issues either with the WordPress core, the theme, or other plugins. Such conflicts can cause parts of your website to malfunction or not display correctly, leading to a poor user experience.
- Maintenance Overhead: More plugins mean more maintenance. Each plugin needs to be updated regularly to ensure compatibility with the latest version of WordPress and to patch any security vulnerabilities. This can become time-consuming and cumbersome, especially if you have a lot of plugins.
- Resource Usage: Plugins consume resources like CPU time and memory. If your hosting plan has limited resources, too many plugins could exhaust those resources, causing your website to be throttled or even taken offline.
- Quality over Quantity: Not all plugins are created equal. Some are poorly coded or no longer maintained. It’s essential to choose high-quality plugins that are actively supported and updated. However, sifting through numerous plugins to determine which are necessary and well-crafted can be a daunting task.
To mitigate these issues, it’s advisable to:
- Only use plugins that are absolutely necessary for your site’s functionality.
- Regularly review and audit your plugins to ensure they’re all in use and up to date.
- Opt for high-quality, well-supported plugins, even if they cost more.
- Consider custom development for specific functionalities to reduce the number of plugins needed.
This approach not only enhances the performance and security of your WordPress site but also improves the overall site management experience.
Related Videos:
Related Posts:
Should I Choose a Hosted or Non-hosted Blogging Platform?
Introduction to Python Programming Language
How To Start a Blog – Beginner’s Guide for 2020
Change the Author Permalink Structure
Google admits it forgot to tell users about its hidden spy microphone
How to Update: Automatically create media_buttons for shortcode selection
Disable the Admin Bar in WordPress
Coordinate Links and Content for Success
What’s behind this 1,000-character phishing URL?
Google is about to have a lot more ads on phones
Kodi-04-2019 No Limits Wizard Magic Build for Kodi 18 Leia
Increase User Engagement & Why It Matters for SEO
Fake Google reCAPTCHA used to hide Android banking malware
What is the use of New Keyword in VB.NET?
Show Post Thumbnails in RSS Feed – WordPress
WordPress, SEO, and Social Media Marketing
How to make a Go-Back Input button with inline JavaScript
How do I install plugins in WordPress?
How do I create money online using WordPress?
Choose your preferred blog CMS platform
WordPress by the Numbers Facts
How do I start a WordPress blog? (hosting wordpress)
Redirect New Registered Users to a Specific Page – WordPress