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.

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

  • Thanks for that.

    This is what I use for that:

    <a href="" title="">


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

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

  • Pingback: Project Spotlight: Crandall Coffee Roasters | The Web Design Blog()

  • 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


      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.



  • Jimbo

    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!!

    • Libby Fisher


      Here is a link to my blog post on how to do what you are trying to do – http://icode4you.net/wordpress-tricks-display-a-list-of-the-titles-of-your-most-recent-blog-posts-from-certain-categories. Just copy and paste the code in that post into the correct template file. Let me know if you have any problems doing that. Thanks!

      • 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

          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 Libby! using that simplie pie instructions worked. I got the error regarding the cache directory but also resolved that.

            Was wondering, instead of putting the “blog posts” on a webpage, can you put the “comments” on the webpage?

          • Libby Fisher

            Hi Tommy,

            Here is what I would try.

            Use http://yourwebsite.com/comments/feed instead of http://yourwebsite.com/feed at the top of your php page, to pull in content from the comments feed rather than the posts feed of your blog.

            Please try that and let me know whether or not it works.


  • Seppe


    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?


    • Libby Fisher


      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! 🙂

  • paul

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


    • Libby Fisher

      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.


  • Nate

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

  • Jon

    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?


    • Jon

      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?


      • Libby Fisher

        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??


        • Jon

          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!

          • Libby Fisher

            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.


  • Chris

    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?



    • Libby Fisher

      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! 🙂

  • Dan

    Thanks this has been a great help! 😀

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

  • 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

      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! 🙂

  • Sarah

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

    • thelibzter

      Hi Sarah,

      I am glad to hear that, thanks for commenting!!


  • I see this is setup for a static page within a wordpress site (like homepage). Is there a workaround where I could place the code into a static html or php page. For example – if main site is built with static pages and WP is installed in the blog directory?

  • Pingback: Post static ( ) | قهوه داغ!()

  • mehul patel

    how to display wordpress web site blog in php web site home page?

  • mehul patel

    how to display wordpress web site blog in php web site home page?

  • somu

    iam getting this error

    Fatal error: Call to undefined function get_posts() in /home/vdecor/public_html/x.php on line 111


  • Dan Foggleman

    affiliateswitchblade.com/blog/postsscroller That allows you to put your blogs latest posts on just about ANY website there is wordpress or not.

  • Miriam Kahlert

    Hey there,
    im new with wordpress, does this show the whol Post ?
    and how do I Embett this?

    • Josphat Rasugu

      It shows the whole post, use instead of

  • Innocent Urum

    am i supposed to copy below code to my static homepage directly or throught the database?

    • Josphat Rasugu

      if you want the posts to show on the home page, you have to make a template which contains the above code, then go to your wordpress control panel and create a home page if not available, or you can make any page your home page. Make sure the page you have selected to be your home page uses the template which contains the above code. http://www.wpbeginner.com/wp-themes/how-to-create-a-custom-page-in-wordpress/

  • Deni Hamdani

    thanks man, it works …

  • Eawedat

    Is it correct syntax?

  • Josphat Rasugu

    Working on static pages, I also used it to show latest post on sidebar.

  • Hayley

    Hi Libby, I’d really like to add my most recent post to my homepage using this code, but I’m a complete novice when it comes to code and I don’t know where to copy this into or anything.
    Could you help me?

  • BS team

    didn’t work for me

  • jack glendening

    FYI “Displaying WordPress Posts on a Separate Web Page” link is broken – would really like to view that since it hopefully matches what I am trying to do