get_next_posts_page_link()
Retrieves the next posts page link.
Backported from 2.1.3 to 2.0.10.
No Hooks.
Return
String|null
. The link URL for next posts page.
Usage
get_next_posts_page_link( $max_page );
- $max_page(int)
- Max pages.
Examples
#1 Display the button "Show more" with url
For example, we have a post type called figurants
and in this archive, we want to display a button to the next page of the pagination.
Let's say we are now on pagination page 2:
<?php if ( $next_url = get_next_posts_page_link( $GLOBALS['wp_query']->max_num_pages ?: 1 ) ): ?> <button class="btn" type="button" data-url="<?= $next_url ?>">Show more </button> <?php endif; ?>
We get it:
<button class="btn" type="button" data-url="https://example.com/figurants/page/3/">Show more </button>
Notice how the check is done. This is because if the function does not specify the maximum number of pagination pages or specify 0
, it will assume there is an infinite number and always return a link even if the next pagination page does not actually exist. When you use WP_Query{} (this includes the basic query), the property max_num_pages
contains the maximum number of pagination pages, but if there is only one pagination page, it will be 0
, we have to replace it with 1
to make get_next_posts_page_link() function work correctly.
Notes
- Global. Int. $paged
Changelog
Since 2.0.10 | Introduced. |
get_next_posts_page_link() get next posts page link code WP 6.7.2
function get_next_posts_page_link( $max_page = 0 ) { global $paged; if ( ! is_single() ) { if ( ! $paged ) { $paged = 1; } $next_page = (int) $paged + 1; if ( ! $max_page || $max_page >= $next_page ) { return get_pagenum_link( $next_page ); } } }