The next_posts() WordPress PHP function displays or retrieves the next posts page link.
Usage
next_posts($max_page, $display);
Custom Example:
Input: next_posts(2, true);
Output: Displays the link to the next posts page.
Parameters
$max_page
(int) – Optional. The maximum number of pages. Default is 0.$display
(bool) – Optional. Whether to echo the link. Default is true.
More information
See WordPress Developer Resources: next_posts()
Examples
Display the Next Posts Link
This example will display the next posts page link.
next_posts();
Retrieve the Next Posts Link
This example will retrieve the next posts page link without echoing it.
$link = next_posts(0, false); echo $link;
Limit the Next Posts Link
This example will display the next posts page link, but limit the maximum number of pages to 3.
next_posts(3);
Display Next Posts Link with Conditional Check
This example will display the next posts link only if there are more pages.
global $wp_query; if ($wp_query->max_num_pages > 1) { next_posts(); }
Customizing the Next Posts Link Text
This example will display the next posts link with custom text “View More Posts”.
add_filter('next_posts_link_attributes', 'custom_next_posts_link_attributes'); function custom_next_posts_link_attributes() { return 'class="view-more-posts"'; } next_posts();