Display the Latest Posts with WordPress

If you set a static page as your front page, it can be a little tricky figuring out how to link to a page that shows your latest posts. So far I have come across two solutions you can use with your WordPress blog when linking to such a page.

Solution One

Create a link in your sidebar or somewhere on your site that looks like this: http://www.yourdomain.com/?p=all You should replace the http://www.yourdomain.com/ with the location of your blog.

Solution Two

You need to create a new page template by copying an existing one. In your new page template, find this line:

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

Then replace that line with this code:

<?php $my_query = new WP_Query( 'showposts=10' );

while ( $my_query->have_posts() ) : $my_query->the_post();

$do_not_duplicate = $post->ID; ?>

The code you just added creates a loop and retrieves the latest 10 posts and will display them on a page you create with your new template.

If you know of another solution to accomplish this task, please share it with my readers and me by commenting below.

20 thoughts on “Display the Latest Posts with WordPress”

  1. Hi,
    Question: If I want to put a or whatever to a page other than wordpress, eg. another website or another directory of the same domain, to show the latest several posts, what should I do with the html/php code? I definitely cannot use the wordpress functions…

  2. This was also exactly what I was looking for, but I had to add a bit to the script. Might be obvious to some, but this is how I set it up:

    have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;?>
    <a href=””>

  3. Hi Josh!

    Thank you so much for the instructions. I’ve been looking for a solution so long… I’m really grateful for this!

    But I’m still not 100% satisfied, I wish I could have a “Previous Entries” link or any other kind of pagination. Any idea to accomplish that?

    Thanks,
    Stefan

    1. Stefan,

      If you are wanting pagination, try this code (do note the use of query_posts instead of WP_Query):

      <?php
      $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;

      query_posts(‘showposts=10&paged=’.$paged);

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

      <h2><?php the_title(); ?></h2>

      <?php
      endwhile;

      posts_nav_link();
      ?>

  4. Thanks for the instant answer, Josh! I’ve tried it bot got the following error:

    Parse error: syntax error, unexpected ‘=’ in /home/.sites/84/site989/web/blog/wp-content/themes/clasictone/pageofposts.php on line 28

    at this line

    query_posts(โ€™showposts=10&paged=โ€™.$paged);

    Any idea?

    1. Stefan, I think the problem has to do with the single quotes. If you copy and pasted the code I provided, then delete the single quotes and retype them. I think that should fix the parse error.

      1. Ah yes, stupid me…

        Good, that works now. Now, the page looks a little different – post titles are no links and there are no post dates. Guess that’s because of the different query, right?

  5. Just want to say a little thank you for taking the time to post this solution, its brilliant! Even though i have used it with your nav code, it works like a charm! Honestly like many others, i have been looking for this solution for months!

    @Stefan If you have copied your index.php file in order to make a new template from it, the easiest way to include everything is to copy everything below the line:

    <?php while (have_posts()) : the_post(); ?> (NOTE: U MUST DELETE THIS LINE OF CODE AFTER YOUR DONE COPYING)

    keep copying everything and STOP just above this line:

    <?php endwhile; ?> (NOTE: U MUST DELETE THIS LINE OF CODE AFTER YOUR DONE COPYING)

    Now refering back to Joshs code he kindly made for you in the comments section (By the way his code needs to go below this line): <?php if (have_posts()) : ?>

    and from his code just replace this line: <h2><?php the_title(); ?></h2>

    with everything you just copied and thats it.

    p.s I dont know what order you going to do this in but hopefully it helps somewhat and good luck LOL.

  6. I am glad you could help me too! Yeah about my code being eaten up, ha-ha do your comments take any PHP code at all? or does it need any special opening and closing PHP code tags. I was hoping to re-write my comment.

  7. Thanks for the post and all the comments. This did solve a question which just popped up 30 minutes ago. If all problems / challenges were solved this quick…

  8. I thinks this is helpful but I get a problem with lastest posts in my website when I try set sticky posts. All sticky posts show in it. Pls help.

Comments are closed.