Is it possible to add a first and latest posts link?

I’m using the following code to show one post in a page (Blog pages show at most is set to 1 post):

<?php
/**
 * Template Name: Front Page
 * @package WordPress
 * @subpackage Prominent
 * @since Prominent 1.0
 */
get_header(); ?>
<div id="tagline">
    <div class="container">
    </div><!-- .container -->
</div><!-- #tagline -->
<div id="content">
    <div class="container">
        <div id="mainbar">
            <?php while ( have_posts() ) : the_post(); ?>
                <div class="content-block-2">
                    <?php the_content(); ?>
                </div>
            <?php endwhile; ?>
            <?php /* Display navigation to next/previous pages when applicable */ ?>
            <?php if (  $wp_query->max_num_pages > 1 ) : ?>
                <?php next_posts_link( __( '&larr; Older posts', 'twentyten' ) ); ?>
                <?php previous_posts_link( __( 'Newer posts &rarr;', 'twentyten' ) ); ?>
            <?php endif; ?>
        </div><!-- #mainbar -->
    </div><!-- .container -->
</div><!-- #content-bottom -->
<?php get_footer(); ?>

So now I got a prev and next link in my front page. I would like to know if is possible to add a first and latest posts link?

EDIT:

By First and Latest posts I mean, for instance,

Since I only allow one post per page, only one post will be shown.

I would like a link for post number 1 and 8 in the example below:

1 2 3 4 5 6 7 8

2 Answers
2

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' );

Output

enter image description here

Leave a Comment