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).
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?
The way I retrieve posts on my blog is to use the following:
Then you would go and create the code to control the display of each post. So for a really simple example:
Then at the end of the posts you just have to end your while loop:
Just to be sure – you are not using
Reading
settings at all here? Just regular home page and regular static page?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.WP_Query
orget_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.Front page
with your static content andfront-page.php
template.Posts page
that will automatically use default Loop for latest posts or can be customized withhome.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.I’m not sure it’s exactly what you need, but maybe you’d like to take a look on my workaround. It’s a function that displays a list of the posts wherever you want, with thumbnails for the first image of the post.
http://wpworks.wordpress.com/2011/02/01/display-wordpress-post-list-with-custom-size-thumbnails/
I hope you find it useful.
Best Regards,
Alvaro