Using WordPress ‘next_posts()’ PHP function

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

This example will display the next posts page link.

next_posts();

This example will retrieve the next posts page link without echoing it.

$link = next_posts(0, false);
echo $link;

This example will display the next posts page link, but limit the maximum number of pages to 3.

next_posts(3);

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();
}

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();