How to display latest posts in wordpress page

Seems simple, and it is, but sometimes you just want a little control over displaying content and posts to your users instead of relying just on plugins and themes, etc. Well, how to display recent posts in WordPress, as a list, is a good way to start.

PHP

Since our goal for this how-to is to display latest posts as a list in php, I’ll assume you know how to add html where you need it:




<?php
$recent_posts = wp_get_recent_posts(10);

foreach($recent_posts as $post){
    if($post["post_status"] != “draft”) {
       echo “do your output here”;
    }
}
?>

This code will get the 10 latest posts (which is default) into your array for looping.

I also added a condition to make sure the post is not a draft, as I’m using this code as the posts loop on the home page of this blog.

That’s it for now … more to come later.

Happy coding!

 

Comments are closed.