get_previous_posts_page_link()
Gets the URL for the list of posts published later than the current ones (pagination link).
Since posts are usually sorted in reverse order (later posts are shown first), this function typically directs to earlier posts.
Works on all pages except is_single(). That is, the condition is_single() should not trigger.
Works based on the global variable global $paged;.
- Use get_next_posts_page_link() when you need to get a link to later (earlier) posts.
- Use get_previous_posts_link() when you need to get the ready HTML for the list of posts published later.
Uses: get_pagenum_link()
Used By: get_previous_posts_link()
1 time — 0.0017691 sec (very slow) | 50000 times — 5.09 sec (fast)
No Hooks.
Returns
String|null. URL of the archive of posts published earlier than the current ones.
Usage
get_previous_posts_page_link();
Examples
#1 Output a link in our button
For example, we have a post type "figurants" and we want to display a button for the next pagination page in this archive.
Let's say we are currently on pagination page 3:
<?php if ( $next_url = get_previous_posts_page_link() ) { ?>
<button class="btn" type="button" data-url="<?= $next_url ?>">
Show previous
</button>
<?php } ?>
We will get:
<button class="btn" type="button" data-url="https://example.com/figurants/page/2/"> Show previous </button>
Notes
- Global. Int. $paged
Changelog
| Since 2.0.10 | Introduced. |
get_previous_posts_page_link() get previous posts page link code WP 6.9
function get_previous_posts_page_link() {
global $paged;
if ( ! is_single() ) {
$previous_page = (int) $paged - 1;
if ( $previous_page < 1 ) {
$previous_page = 1;
}
return get_pagenum_link( $previous_page );
}
}