WordPress 4.1: Using the_posts_pagination() for Post Navigation

the_posts_paginationwordpress paginationwordpress 4.1 functionscustom paginationtheme development
Published·Modified·

The the_posts_pagination() function was introduced in WordPress 4.1. It is commonly used for pagination navigation on post-related pages. Before version 4.1, pagination required two separate functions, but this new function simplifies the process and allows for greater control over the output. The official documentation also provides a function to retrieve pagination variables: get_the_posts_pagination(). This article documents this useful function.

the_posts_pagination( array(
    'base' => '',  // You can customize the navigation URL; usually not needed
    'prev_text' => 'Previous', // Title and link for the previous page (default: Previous)
    'next_text' => 'Next',     // Title and link for the next page (default: Next)
    'before_page_number' => '', // Number before the page number
    'after_page_number' => '',  // Number after the page number
    'total' => 5,               // Control the number of pages displayed (default: max pages)
    'format' => '?paged=%#%',   // Format for navigation links, visible on hover
    'current' => max( 1, get_query_var( 'paged' ) ), // Current page number
    'add_fragment' => ''        // String appended to pagination links on hover
) );

This function is quite powerful. Below are the common parameters with comments for developers to reference. It is typically used in WordPress theme files such as index.php, category.php, or archive.php for article list pagination, and it does not need to be placed inside the main loop.

Official Documentation: https://developer.wordpress.org/reference/functions/the_posts_pagination/

Original source: WordPress 4.1 - Common Function the_posts_pagination (Directory Pagination) – ThemeHello. All rights reserved by the original author. For copyright issues, please contact QQ: 337003006 for removal.