Limit to One Post on WordPress Front Page

Let’s say you have a WordPress blog. You only want one post to appear on the front page of that blog. But, you want all other pages (e.g., the second page, category pages, monthly archives, etc.) to display more than one (be it 5, 10, or whatever you chose on your “Reading Settings” page). The first part is easy, the latter has a few pitfalls to be aware of.

First, we’re going to call query_posts to modify the query a bit. We only want this on the home page, so we wrap it in an if statement:

global $wp_query;
if ( is_home() && !is_paged() ) {
  query_posts(
    array_merge(
      $wp_query->query,
      array('posts_per_page'=>1)
    )
  );
}

is_home() will be TRUE if you’re on the main blog page; !is_paged() verifies that we’re on the first page (because is_home() is still TRUE on subsequent pages (e.g.http://example.com/page/2/)).

If you left it at that, though, http://example.com/page/2/ would skip over several posts (those that would have been on the front page if you hadn’t modified the query). So we need to offset the query on all subsequent pages:

elseif ( is_home() ) {
  query_posts(
    array_merge(
      $wp_query->query,
      array(
        'paged'=>($wp_query->query['paged']-1),
        'offset' => (get_option('posts_per_page')*($wp_query->query['paged']-2))+1
      )
    )
  );
}

Here we basically tell the query to back up a full page (the paged argument) and set our offset to start us at the post we want.

Put the two pieces together and paste it at the top of index.php in your theme directory and you should be all set. Here’s the full code in one chunk:

global $wp_query;
if ( is_home() && !is_paged() ) {
  query_posts(
    array_merge(
      $wp_query->query,
      array('posts_per_page'=>1)
    )
  );
  $GLOBALS['wp_the_query'] =& $GLOBALS['wp_query'];
} elseif ( is_home() ) {
  query_posts(
    array_merge(
      $wp_query->query,
      array(
        'paged'=>($wp_query->query['paged']-1),
        'offset' => (get_option('posts_per_page')*($wp_query->query['paged']-2))+1
      )
    )
  );
  $GLOBALS['wp_the_query'] =& $GLOBALS['wp_query'];
}

Update: If you use a widget that calls wp_reset_query() while in the loop (yes, there are reasonable use cases for that), you might need to include this line after the above code:

$GLOBALS['wp_the_query'] =& $GLOBALS['wp_query'];

This will preserve the changes you made in query_posts() if wp_reset_query() is called.

Update 2: Fixed some errors in the code that were making it fail horribly.

2 Responses to Limit to One Post on WordPress Front Page

  1. Durga Thakur says:

    Thanks a lot.

    I searched for this on many sites but not found any solution.

    Great post.

    Thanks again.

  2. peter says:

    Thanks for this, this is great. However, it appears that the second to last page doesn’t have a link to the last page. I’m not a coder, so I’d appreciate any suggestions.

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">