get_next_posts_link()
Retrieves the next posts page link.
Hooks from the function
Return
String|null
. HTML-formatted next posts page link.
Usage
get_next_posts_link( $label, $max_page );
- $label(string)
- Content for link text.
Default: null - $max_page(int)
- Max pages.
Examples
#1 Basic use
<?php echo get_next_posts_link(); ?>
#2 Custom link text
<?php echo get_next_posts_link( 'Next page' ); ?>
#3 Custom text and number of pages
<?php echo get_next_posts_link( 'To the next page', 4 ); ?>
#4 Using together with the WP_Query arbitrary loop
Add $max_pages parameter when you create a loop with WP_Query. To get the number of all pages, you can use "max_num_pages" property of WP_Query object:
<?php // define the current pagination page // use the 'page' parameter instead of paged on the main page if it is static $paged = get_query_var( 'paged' ) ?: 1; // query: output posts from category 1 $the_query = new WP_Query( 'cat=1&paged=' . $paged ); if ( $the_query->have_posts() ){ // loop while ( $the_query->have_posts() ){ $the_query->the_post(); // set global variable $post the_title(); } // get_next_posts_link() with page limit (second parameter) echo get_next_posts_link( 'Early posts', $the_query->max_num_pages ); echo get_previous_posts_link( 'New posts' ); // Clean up global variables wp_reset_postdata(); } else { echo '<p>No posts matching the query were found.</p>'; } ?>
Notes
- Global. Int. $paged
- Global. WP_Query. $wp_query WordPress Query object.
Changelog
Since 2.7.0 | Introduced. |