How to get the list of posts in a static page other than front page?

So, for the thousands using WP as CMS, a typical approach is that of using the ‘A Static Page’ option from the Settings > Reading admin page.

However, I’m in a different scenario: our front page is displaying static content (home.php template drives that), and we have a secondary static page (called News) which should display the list of most recent posts (what you usually find on an average blog’s front page).

Read More

I set up the News page to use a custom template (page-NewsIndex.php); based on TwentyTen’s archive.php template, this file displays a header, calls rewind_posts() and then calls get_template_part(‘loop’, ‘newsindex’) so that we end up in loop.php (or loop-newsindex.php, if it exists). Peachy.

Loop.php has your typical loop structure (again, based on TwentyTen’s loop.php template – tweaked to simplify since we don’t need 3 type of loops):

<?php while ( have_posts() ) : the_post(); ?>

However, when we access the page, this loop seems to use the current URL to determine the posts to display, as if the News page was defining a category – which is not the case for us.
What would be the appropriate query_posts for me to use to simulate the query_posts that WP usually runs for you when you get to the front-page of a typical blog?

Related posts

Leave a Reply

3 comments

  1. The way I retrieve posts on my blog is to use the following:

    <?php $recentPosts = new WP_Query(); $recentPosts->query('showposts=5&cat=CAT_ID_GOES_HERE'); while($recentPosts->have_posts()): $recentPosts->the_post(); ?>
    

    Then you would go and create the code to control the display of each post. So for a really simple example:

    <h1 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    

    Then at the end of the posts you just have to end your while loop:

    <?php endwhile; ?>
    
  2. Just to be sure – you are not using Reading settings at all here? Just regular home page and regular static page?

    • your News page is static page so its Loop (native WP Loop created from URL) should reflect that (so not sure how it seems to be category);
    • query_posts() is function meant to adjust such native Loop. You shouldn’t use it here, because you would be trying to forcefully change static page into index page which ends up in horrible bugs.
    • you can use custom WP_Query or get_posts() to display some posts on static page, bu likely you won’t get pagination to work.

    Overall I feel like you are reinventing the wheel here. Settings > Reading seems like a perfect match.

    1. Front page with your static content and front-page.php template.
    2. Posts page that will automatically use default Loop for latest posts or can be customized with home.php template.

    This seems exactly like what you are trying to get, no?

    PS TwentyTen loop.php scares me. It looks like usability and common sense were sacrificed for maximum conditional flexibility.