How to display the first and last page in WordPress pagination

first and last page in WordPress pagination

Displaying post pagination in WordPress is very easy. But how can you display links for the first and last page in WordPress pagination?

Table of contents


What is post pagination in WordPress

The WordPress pagination displays a paginated navigation to next/previous set of posts, when applicable. In other words, it helps the users to navigate between pages in the archive of your website.


How to display post pagination in WordPress

To display post pagination in WordPress you just need to use the the_posts_pagination function, for example:

the_posts_pagination(
    array(
        'prev_text' => '«',
        'next_text' => '»',
        'mid_size'  => 4,
    )
);

This function displays the previous and next links, but not the first and last

WordPress pagination

You may need the first and last links for a better user experience, so let’s see how you can do it.


How to display the first and last page in WordPress pagination

To display the first and last page in WordPress pagination you need to write custom href links before and after the pagination. To display a custom page link in WordPress you can use the get_pagenum_link function.


Display the first page link

First of all, you need to check if you are already on the first page. To do that, you need to get the paged variable, using the get_query_var function:

$current_page = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

With this function, you get the current page pagination number. So, you can display the first page link in this way:

<?php
$first_page = 1;

if ( $current_page != $first_page ) {
?>
	<a href="<?php echo get_pagenum_link( $first_page ); ?>" title="First page" class="first-page">
		&laquo; First
	</a>
<?php
}
?>


Display the last page link

Again, you need to check if you are already on the last page, but you need to know how many pages the archive has first. To find that, get the max_num_pages of the current wp_query. For the current wp_query, call:

global $wp_query

So you can display the last page link in this way:

<?php
global $wp_query;
$last_page = $wp_query->max_num_pages;

if ( $current_page != $last_page ) {
?>
    <a href="<?php echo get_pagenum_link( $last_page ); ?>" title="Last page" class="last-page">
        Last &raquo;
    </a>
<?php
}
?>


Complete code

Here is the complete code to display the first and last page in WordPress pagination:

<div class="pagination-wrapper">
    <?php
    global $wp_query;
    $last_page = $wp_query->max_num_pages;
    $first_page = 1;
	$current_page = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

    if ( $current_page != $first_page ) {
    ?>
        <a href="<?php echo get_pagenum_link( $first_page ); ?>" title="First page" class="first-page">
            &laquo; First
        </a>
    <?php
    }

    the_posts_pagination(
        array(
            'prev_text' => '&laquo;',
            'next_text' => '&raquo;',
            'mid_size'  => 4,
        )
    );

    if ( $current_page != $last_page ) {
    ?>
        <a href="<?php echo get_pagenum_link( $last_page ); ?>" title="Last page" class="last-page">
            Last &raquo;
        </a>
    <?php
    }
    ?>
</div>

and the output:

WordPress pagination first – last

I hope that you found this article helpful. Keep coding!

Tags

More Articles

All Articles
All Articles
© Antonis Papadakis 2025. All rights reserved.