What you need are $GLOBALS['wp_query']->max_num_pages
and get_pagenum_link()
. Then you just have to compare get_query_var( 'paged' )
with max_num_pages
and create a link if they are not equal:
/**
* Link to last page of a paged archive.
*
* @param string $text Link text
* @return string Nothing if we are on the last page, a link otherwise.
*/
function t5_get_last_posts_link( $text="Last Posts" )
{
global $wp_query;
if ( // something is very wrong
! isset ( $wp_query->max_num_pages )
// there is just one page
or 1 == $last = $wp_query->max_num_pages
// we are already on the last page
or get_query_var( 'paged' ) == $last
)
{
return '';
}
return sprintf( '<a href="https://wordpress.stackexchange.com/questions/12531/%1$s">%2$s</a>', get_pagenum_link( $last ), $text );
}
/**
* Link to the first page of a paged archive.
*
* @param string $text Link text
* @return string Nothing if we are on the first page, a link otherwise.
*/
function t5_get_first_posts_link( $text="First Posts" )
{
global $wp_query;
if ( // something is very wrong
! isset ( $wp_query->max_num_pages )
// there is just one page
or 1 == $wp_query->max_num_pages
// we are already on the first page
or 2 > (int) get_query_var( 'paged' )
)
{
return '';
}
return sprintf( '<a href="https://wordpress.stackexchange.com/questions/12531/%1$s">%2$s</a>', get_pagenum_link( 1 ), $text );
}
Usage
print t5_get_first_posts_link( 'Newest' );
print t5_get_last_posts_link( 'Oldest' );