WordPress: Displaying your latest blog post on a static page

Here is  the simple and handy snippet of code that I use to display a WordPress blog’s most recent post on a static home page.

<?php
$args = array( 'numberposts' => 3 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
	<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
	<?php the_content(); ?>
<?php endforeach; ?>

You can see an example of this in action at one of the websites I designed – CrandallCoffee.com.

If you are trying to display your most recent post on a web page of a completely separate website (not a WordPress website), please take a look at how to do this in my post  Displaying WordPress Posts on a Separate Web Page.

And, as always, let me know if you have any questions about this.

2
Shares

30 Comments

  1. Marja says:

    Thanks for that.

    This is what I use for that:

    <a href="" title="">

    |

    <a href="">
    <a href=""> »

  2. Yes this is HTML & PHD based code.I like this kind of post which tell us much important massage.thank you again.

  3. Thanks for the info, I tried it and it didn’t work. I’ve tried other snippets of code and it just displays the first two lines and then messes up my menu. I’m guessing its my theme and I’m doomed to not have a recent post display on my static homepage.

    Unless I’m supposed to put the code snippet in my theme files.

    • Libby Fisher says:

      Hi,

      Yes, you are supposed to put the code snippet in the template file, so that could very well be the reason it is not working for you yet. Please feel free to post or email me (libby@libbyfisher.com) a link to the page or website you are working on and I will try to help you get it working.

      Thanks!

      ~Libby

  4. Jimbo says:

    I just want to create a list of the 8 most recent post titles on a static homepage (i.e not any of the actual post – just the titles) Is this the sort of code that could be modified to show only titles? Also which file would it go in?

    Any help would be appreciated!!

  5. tommy says:

    hey there, newbie here.

    Main page: http://www.mathgamecool.com

    blog is at: mathgamecool.com/mathgames/

    I must be missing a few steps cuz you don’t just post that code you gave into my index.html right? the recent posts say to put it into the templates files? whats that?

    • Libby Fisher says:

      Hi Tommy,

      It doesn’t look like your website is built on WordPress, in which case the code in this post will not work for you. This code is used to display your blog posts on a static page on the same WordPress site as the blog is on.

      For what you are trying to do, please check out this post – http://libbyfisher.com/displaying-wordpress-posts-on-a-separate-web-page. That should help you to display your blog posts on your home page. Please let me know if you run into any issues and I would be happy to help.

      Thanks!

  6. Seppe says:

    Hi,

    Nice piece of code! I would like to use this on a static home page but with a small thumb on the left..(to make it more fancy). Is this possible?

    Thanks!

    • Libby Fisher says:

      Hi,

      I would try adding this line of code:

        < ?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>

      So, your entire block of code would look like this:

      < ?php
      $args = array( 'numberposts' => 3 );
      $lastposts = get_posts( $args );
      foreach($lastposts as $post) : setup_postdata($post); ?>
      ">< ?php the_title(); ?>
       < ?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
      	< ?php the_content(); ?>
      < ?php endforeach; ?> 

      Please let me know if that doesn’t work for you. Thanks! :)

  7. paul says:

    into which template file do I need to paste the above code for it to work ?

    thanks

    • Libby Fisher says:

      Hi Paul,

      You should place it into your home.php template file (you may want to create one if your theme doesn’t have on built in), or whichever template file your home page uses. If your home page uses the same template file as other pages on your site (i.e. one-column.php) you will probably want to use a conditional tag along with the code in the post above to make sure that the most recent post only shows on the home page. Please let me know if that doesn’t make sense.

      Thanks!

  8. Nate says:

    What if I want only a certain category to be displayed? I currently have a theme that does not display a category menu, only pages. I have my settings to display static home page and blog posts on my “Events” page. But I need to separate my events. Any suggestions? One forum suggests creating a few page templates and use the “single_post.php” file code as the base code and modify it accordingly. Then it will show up as a template option in the admin when I go to create the page. I just dont know how to modify the php file to do this for each category (3 total).

  9. Jon says:

    Hi Libby,

    Really helpful stuff! Thanks so much. I’m having a bit of trouble with a site I’m working on: davis.seventhmovement.com

    I want to have a few dynamic posts below some static content, but when I use the code above if shows below the footer of the site as you can see. Any suggestions?

    Best,
    Jon

    • Jon says:

      worked out a bit of it using a widgets on pages plug in, an exec php plugin, and your code :)

      But I’m still having an issue where it says “Home” above each individual blog post. Any suggestions on that one?

      Gracias,
      J

      • Libby Fisher says:

        Hi Jon,

        Hmm, what are the titles of each of your posts?? Did you change the code in the post above or are you using it as-is??

        Thanks!!

        • Jon says:

          Hey Libby,

          The titles are Test post 1 and test post asdf, or something close to it. It seems like it’s happening because I put the code in a text widget instead of the page php. The code is also the exact same as above

          Your original instructions would work great if the posts would show up on the body of the page instead of below the footer.

          Thanks for the help!
          Jon

          • Libby Fisher says:

            Hi Jon,

            I am sure you can get the posts to display in the correct spot on the page, it is probably just a matter of where you are placing the code in the php page and also maybe a styling issue. Please let me know if you are still having trouble doing that and I can take a look.

            Thanks!!

  10. Chris says:

    Hi Libby,

    Do you have any ideas about embedding a plugin sidebar widget into a static html page? The site I’m designing is mostly static html with a subdirectory with WordPress installed to take advanatge of some cool calendar plugins. I have no problems having a separate wordpress page with the complete calendar but it would be great to be able to embed the calendar widget into the static home page. Any ideas?

    Thanks,

    Chris

    • Libby Fisher says:

      Hi Chris,

      I’m sorry but I don’t know off the top of my head how you would do that :-/ hope you figure it out!!! let me know if you do, I’d be interested to know how you did it. Thanks! :)

  11. Dan says:

    Thanks this has been a great help! :D

  12. Hey Dude, Thanks for the code. Given example page “CrandallCoffee.com” is looking very simple and attractive.

  13. Hi Libby,

    First of all thanks for your code and replies. However, I am not sure if I want to use that code because I can change separate post page and home page from reading settings of wp dashboard. The theme I am using also have two different templates for homepage and post page. I am assuming this code is for displaying static homepage and dynamic post page merged in a common page template. Is that so?

    My homepage already contains a default feature box with one article. But I would love to show some other recent articles in the homepage as well. Will using the code make any collision with the homepage feature box?

    Looking forward to your reply. Keep up the good work :)

    p.s.: I am new to wordpress.

    • thelibzter says:

      Hi Adnan,

      I dont believe the code will interfere with the feature post box, but if it does, let me know and we can try and come up with a different solution. Thanks for commenting! :)

  14. Sarah says:

    Just wanted to stop and say THANK YOU for this code…I have been searching forever for something to do this, so thanks!

  15. Leave a response

    Current ye@r *