WordPress Front Page Single Post

Posted by on September 11, 2009

Well, I’ve rejigged my site theme, and I decided to play a bit with the layout.

The look of the site seemed to be better with just the excerpt from a single post on the front page.

Now, that sounds really straight forward and something that WordPress should be able to do out of the box. And it is.

However, if you want the “Older Entries” pages to then show, say, 10 posts you’re knackered as all subsequent pages will just show 1 entry. I tried the Custom Post Limits plugin, but it still didn’t work as page 2 started at post 6.

After reading a lot of entries on this topic (it seems I’m not the only person keen on this!) and getting nowhere fast. Lots of “read this link then work it out yourself” type replies I finally got something to work. Using both the offset AND paging attributes of the query_posts() function!

The only issue with this is that the last page wouldn’t show at all. This was due to the fact that the front page misses all but 1 of the entries, then the 2nd page starts at entry 2. In effect you’re missing a number of entries up to the usual number per page minus 1 – everything that would be on the last page. To get round this, I simply double the number of entries displayed on what WordPress believes is the final page.

To get this to work, add the following snippit to the top of the home.php file in your theme directory.
If you don’t have a home.php file then copy your index.php in your theme directory to home.php which will then take precedence according to the WordPress Template Heirarcy. I do not recommend adding this to your index.php file.

<?php
//WATCH THAT LINE WRAP!
if (is_home()) {
        $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
        if ($page == 1) {
                query_posts("showposts=1&paged=$page");
        } else {
                $numposts = get_option('posts_per_page');
                //Work out what posts to show first on the page.
                $offset = (($page -1) * $numposts) - ($numposts -1);
 
                //Quick hack to display double the number of posts on the last page as the offset hides posts per page -1.
                if ($page == $wp_query->max_num_pages) {
                        $numposts = 2* get_option('posts_per_page');
                }
                query_posts("offset=$offset&paged=$page&showposts=$numposts");
 
        }
}
?>

Tags: , , ,

Comments (6)

  • This works like a charm! Thanks!

  • It works well and thanks but WP-PageNavi plugin doesn’t work. Page numbers get wrong.
    I beleive there could be solution for that but I didn’t have time to check, and I’m a newbie for WP.
    Thanks again.

  • Works. Now about the five pounds. So I owe you five quid. All right. I’m going through sort of a sticker shock. Let’s leave it up, working, for a bit. If it’s still running successfully 90 days from today, I will pay you in full. Yours, with gratitude.

  • Forex Software und Signals Usage

  • My brother recommended I may like this website.
    He was totally right. This submit truly made my day. You cann’t consider simply how a lot time I had spent for this info! Thanks!

Do you have anything to say?

Powered by Wordpress and Stripes Theme Entries (RSS) | Comments (RSS)