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.

  • http://www.marbiedesign.nl/en Marja

    Thanks for that.

    This is what I use for that:

    <a href="" title="">

    |

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

  • http://www.seoservicesindia.mobi SEO Services India

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

  • http://www.asharperrazor.com A Sharper Razor

    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

      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

  • 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

      Hi,

      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!

      • http://www.mathgamecool.com tommy

        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!

          • http://mathgamecool.com tommy

            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.

            Thanks!

  • Seppe

    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

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

  • paul

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

    thanks

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

      Thanks!

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

    Best,
    Jon

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

      Gracias,
      J

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

        Thanks!!

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

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

            Thanks!!

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

    Thanks,

    Chris

    • 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! :)

  • http://www.danielfurze.co.uk Dan

    Thanks this has been a great help! 😀

    • http://www.emdiworld.com/dubai/ Diploma In Events Management

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

  • http://www.bdwebsites.com Adnan Tanvir Reza

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

      ~Libby

  • http://www.madrat.com Energy Drink Dude

    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?