The next_posts_link() WordPress PHP function displays the next posts page link.
Usage
next_posts_link($label, $max_page);
Example:
next_posts_link('Older Entries', 3);
Parameters
$label
(string) Optional: Content for link text. Default: null$max_page
(int) Optional: Max pages. Default: 0
More information
See WordPress Developer Resources: next_posts_link
Examples
Basic Usage
Display a basic next posts link.
next_posts_link('Older Entries »', 0);
Check if Next Link Exists
Check if the next link exists before displaying it.
if (get_next_posts_link()) : next_posts_link('Older Entries »', 0); endif;
Using with WP_Query
Add the $max_pages
parameter to the next_posts_link() function when querying the loop with WP_Query. To get the total amount of pages, you can use the max_num_pages
property of the custom WP_Query object.
// Set the "paged" parameter $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // The query $the_query = new WP_Query(array('cat' => 1, 'paged' => $paged)); if ($the_query->have_posts()) : // The loop while ($the_query->have_posts()) : $the_query->the_post(); the_title(); endwhile; // next_posts_link() usage with max_num_pages next_posts_link(__('Older Entries', 'textdomain'), $the_query->max_num_pages); previous_posts_link(__('Newer Entries', 'textdomain')); // Clean up after the query and pagination wp_reset_postdata(); else: echo '<p>' . __('Sorry, no posts matched your criteria.', 'textdomain') . '</p>'; endif;
Customizing Link Text
Customize the link text for the next posts link.
next_posts_link('View More Posts »', 0);
Limiting Maximum Pages
Limit the maximum number of pages displayed by the next posts link.
next_posts_link('Older Entries »', 5);