Arrow

Show latest posts on a page other than the WordPress main page

First published on January 18, 2009

On WordPress-powered sites, by default the home page shows the latest blog posts. However, on some WordPress sites you might want to show a different page on the main page (at yoursite.com), and show the latest posts on a separate page (like yoursite.com/news). I’ve been asked a few times how to do this, so here’s how.

First, consider that behind every view on a WordPress site is a template. A simplified way of thinking about this is that the home page (which isn’t an actual “page” in the WordPress “post” / “page” system) loads the index.php template in your theme; a blog post loads the single.php template; and a static page loads the page.php template. With one of a few plugins, you can use different templates for specific posts; as for pages, WordPress has built-in support for page-specific templates:

On the sidebar of an add or edit page screen

Typically, the code in the index.php template contains The Loop for the number of posts configured in “Settings” > “Reading” > “Blog pages show at most”. This is what produces the home page of latest posts.

To transport the display of the latest / current posts outside of the home page, first create a specific page (called something like “News” or “Blog” or whatever’s appropriate for your needs) that will display the blog posts. This page can be empty.

Then, you can do one of two things. The more difficult way to do things is to transport the code for “The Loop” into a new page template, and set the “News” or “Blog” page that you created to use that template. Note, however, that “The Loop” is fed posts on the home page, but it is fed a single page on other pages. To feed it posts for use on a page, you must call query_posts directly before the loop. For example, to show the latest 5 posts:

  <?php query_posts('show_posts=5'); ?>
  <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <?php // and so on with The Loop as normal... ?>

Then you would hard-code the index.php template to load whatever custom home page you want.

The simpler, built-in way to do things is to go to the “Settings” > “Reading” admin page:

The 'front page displays' option

Typically, the “front page displays” option is set to “Your latest posts”. However, you can configure it so that you have a custom page for the main / home / front page, and then assign the latest posts to another page. In the settings in the screenshot above, this means that the home page will load the template for the page named “Front” (this would be the page.php template unless a specific template was configured); while the “News” page will load the index.php template, which typically contains “The Loop” of latest posts.

Arrow

20 Responses to “Show latest posts on a page other than the WordPress main page”


  1. George says:

    I managed to transport the code for “The Loop” into a new page template, and then set the “News” page that i created to use that template, but the news page will not show the latest posts, but the news page as a post, meaning inside news page i have "News" as the title, the date at wich news page was created. As if the news page would be the latest post.


  2. Peter says:

    Hi George, try putting something like this before the loop:

    <?php query_posts(‘show_posts=5′); ?>

    (shows 5 posts)


  3. George says:

    Hi! Thanks for your fast answer. I succeeded showing the latest posts with queuing only posts from the news category, but then i have no navigation amoung posts, i can display next and previous links but they won`t work… So the navigation part would be my problem now… Because i only want to show one post at a time.


  4. Rob says:

    Duh….how could I not see that? I always wondered how you would use the drop down for posts? Now I know, and use. Thanks for writing this post.


  5. Billig says:

    <?php query_posts(‘show_posts=5′); ?>

    Yes, it shows 5 latest – thanks!


  6. Jake says:

    Ok, this works for me…but I want to put pagination down the bottom so that the user can choose to view "Older posts" as well. Any ideas how you do this?

    Reply from Peter: You can look at the default WordPress main page template to see how they do “next” and “previous” links for posts. There are also some “paged navigation” plugins for WordPress if you do some searching.


  7. Justin says:

    Thanks dude, helped me out.


  8. Jason says:

    This is exactly what i was looking for and it works perfectly, but one thing… the is_home() function still thinks the index.php is still the home page and not the newly created home page.

    Any ideas on how to fix that? Its important to me because i am using a slightly different look on my home page and i use is_home() to change my header and footer.


  9. Jason says:

    Guess i spoke.. err posted.. too soon. Just did a search and found is_front_page() and it works just fine!

    Thanks!!


  10. shiqianlu says:

    Hello!
    When I use "query_posts()",I find it doesn’t support page navigation. Every page of the index is the same. Could you help me out of the trouble?

    Reply from Peter: There are parameters “showposts” and “offset” that you can use in the query_posts() function to help with page navigation. Check the documentation for more information.


  11. mpan says:

    oh man, i’ve been going crazy trying to figure out why my page wouldn’t show my posts, even though i had all the code. OR SO I THOUGHT.

    THANK YOU for sharing the query_posts solution! that really helped.

    *good karma your way*


  12. Roger says:

    That works, except it uses the page template for the home page.

    If you have a custom template for the home page that interferes with normal post display, you have problems.


  13. Pinto says:

    Thank you so much Peter,

    The simple thing was "big" for me to do.

    i didn’t know that it’s so simple to do !

    Thanks a lot for the tut !

    Best Regs,
    Pinto.


  14. Dan W says:

    Thanks for this post. I was spending hours trying to figure out why the loop wouldn’t work on a page template. Surprising how little code help there is out there for WordPress.


  15. Faithe T says:

    Sweet! This is EXACTLY what I was looking for… and simple too!

    THANK YOU, THANK YOU!


  16. DeaFischer says:

    Hello, I have a homepage that is not a WordPress Blog, and there, in the front page, i wanted to show the recent wordpress blogs.
    For this I call:

    <?php
    // Include WordPress
    define(‘WP_USE_THEMES’, false);
    require(‘/mypath/wp-load.php’);
    query_posts(‘showposts=1′);
    ?>

    Unfortunatelly it does not work and returns an error:
    "Error establishing a database connection"

    Can someone help me in this case???
    Thanx a lot in advance


  17. SABOND says:

    Hi! I have one problem – your code doesn’t work with plugin wich allows to use <!–more–> tag to show as many lines as I want – posts are expanded, but I need them to have just first 3 lines. I have the following code –

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php the_content( ‘… Full New »»’, $more_link_text , $strip_teaser, $more_file ); ?>
    <div style="height: 26px"></div>
    <?php endwhile; else: ?>
    <h1>Woops…</h1><br>
    Sorry, no posts were found.
    <?php endif; ?>
    <?php posts_nav_link(); ?>

    And after adding <?php query_posts(‘show_posts=5′); ?> before all this code posts start to show, but they are expanded. At home page they are OK!

    Reply from Peter: I don’t know how your plugin code hooks into WordPress, so that would be a question for the plugin author.


  18. Ryan Kazinec says:

    I’m sure the individual figured out how to utilize pagination with this but I didn’t see the solution mentioned here so I figured I’d post it here to help others out.

    Replace this query:
    <?php query_posts(‘show_posts=5′); ?>

    With this query:

    <!–Change The Amount of Results You Wish To Display Per Page–>
    <?php
    $offset = "0";
    $no_of_posts = "15"; //Number of posts to display on each page
    if (preg_match(‘/page/’, $_SERVER['REQUEST_URI'])) {
    $uri = explode(‘/’, $_SERVER['REQUEST_URI']);
    foreach ($uri as $key=>$value) {
    if ($value == "") {
    unset($uri[$key]);
    }
    }
    $offset = (array_pop($uri) * 15) – 15; //Both These Numbers Should Match the $no_of_posts
    }
    query_posts(‘posts_per_page=’ . $no_of_posts . ‘&offset=’ . $offset); ?>

    CREDIT for this code: This is not my code please visit the source link below! Special thanks to Jarod Taylor.

    http://jarodtaylor.com/blog/wordpress-ultimate-archive-index-with-pagination


  19. Carlos says:

    many thanks for posting this !!


  20. Eric says:

    Outstanding post thanks for sharing! This should be included clearly in the WordPress Codex.

Speak your mind

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word