Using WordPress ‘get_previous_posts_page_link()’ PHP function

The get_previous_posts_page_link() WordPress PHP function retrieves the previous posts page link.

Usage

$prev_posts_page_link = get_previous_posts_page_link();
echo '<a href="' . esc_url($prev_posts_page_link) . '">Previous Page</a>';

Parameters

  • None

More information

See WordPress Developer Resources: get_previous_posts_page_link

Examples

This code displays a simple “Previous Page” link if there is a previous posts page available.

if ($prev_posts_page_link = get_previous_posts_page_link()) {
    echo '<a href="' . esc_url($prev_posts_page_link) . '">Previous Page</a>';
}

Customize the Previous Page text

This code customizes the “Previous Page” text to display “Older Posts” instead.

$prev_posts_page_link = get_previous_posts_page_link();
if ($prev_posts_page_link) {
    echo '<a href="' . esc_url($prev_posts_page_link) . '">Older Posts</a>';
}

This code adds a custom CSS class “pagination-previous” to the “Previous Page” link.

$prev_posts_page_link = get_previous_posts_page_link();
if ($prev_posts_page_link) {
    echo '<a href="' . esc_url($prev_posts_page_link) . '" class="pagination-previous">Previous Page</a>';
}

This code displays a “Previous Page” link with a left arrow icon using Font Awesome.

$prev_posts_page_link = get_previous_posts_page_link();
if ($prev_posts_page_link) {
    echo '<a href="' . esc_url($prev_posts_page_link) . '"><i class="fas fa-arrow-left"></i> Previous Page</a>';
}

This code combines both get_previous_posts_page_link() and get_next_posts_page_link() functions to create a pagination navigation.

$prev_posts_page_link = get_previous_posts_page_link();
$next_posts_page_link = get_next_posts_page_link();

if ($prev_posts_page_link || $next_posts_page_link) {
    echo '<nav class="pagination">';
    if ($prev_posts_page_link) {
        echo '<a href="' . esc_url($prev_posts_page_link) . '" class="pagination-previous">Previous Page</a>';
    }
    if ($next_posts_page_link) {
        echo '<a href="' . esc_url($next_posts_page_link) . '" class="pagination-next">Next Page</a>';
    }
    echo '</nav>';
}