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

All we need is an easy explanation of the problem, so here it is.

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?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 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(); ?>" rel="nofollow noreferrer noopener"><?php the_title(); ?></a></h1>

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

<?php endwhile; ?>

Method 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.

Method 3

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

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply