1

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?

3 Answers 3

3

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; ?>
3
  • This sounds like it may work for us - I'll give it a shot as soon as I can and let you know (I'll accept the answer as soon as I can confirm it works) - Thank you!
    – FOR
    Commented Oct 22, 2010 at 12:43
  • Finally got around to checking your suggestion and it essentially works great for me. I ended up using query_posts(''); instead of making a new WP_Query instance, but that's just because in my case I really don't need to use the main query object anyway. Thanks for putting me on the right path!
    – FOR
    Commented Oct 23, 2010 at 14:36
  • Glad it worked for you :) Commented Oct 24, 2010 at 17:00
1

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.

1
  • correct: not using the Reading setting at all, for now. I know this sounds very much like the case that the Reading > Front Page setting was meant to cover - might try and rework the theme accordingly if we have no quicker fix - thanks (and yeah, no clue as to why the loop in News acts as a category - was suspecting WP looks at the request URL, finds /News and acts as if this was an archive for the News category)
    – FOR
    Commented Oct 22, 2010 at 12:42
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.