2

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

3
  • 1
    Define first and latest posts links? What exactly would these be a link to?
    – t31os
    Mar 20, 2011 at 11:46
  • @t31os OK please see my EDIT
    – janoChen
    Mar 20, 2011 at 13:21
  • The code you've posted is a page template, with no custom query parameters, there's only ever going to be a single result, the page the template is attached to.
    – t31os
    Mar 20, 2011 at 13:59

2 Answers 2

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="%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="%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

0

It appears like what you need is "pagination". Install WP-PageNavi plugin, and add this in your template file (index.php, archive.php, etc. — whichever is relevant):

<?php wp_pagenavi(); ?>

The plugin's settings are self-explanatory. (HINT! %PAGE_NUMBER%)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.