How to display the 5 latest posts on a WordPress page

To display the 5 latest posts on a WordPress page, you can use the WP_Query class to create a custom loop. Follow these steps:

  1. In your WordPress admin dashboard, go to “Pages” > “Add New” or edit an existing page where you want to display the 5 latest posts.
  2. If you are using the Gutenberg block editor, insert a “Custom HTML” block in your desired location on the page. If you are using the Classic Editor, switch to the “Text” tab.
  3. Add the following code snippet in the Custom HTML block or in the Text tab of the Classic Editor:
// Create a new WP_Query instance with the 5 latest posts
$latest_posts = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 5,
'ignore_sticky_posts' => 1,

// The custom loop
if ($latest_posts->have_posts()) :
while ($latest_posts->have_posts()) : $latest_posts->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>
<?php endwhile;
else :
echo '<p>No recent posts found.</p>';
  1. Publish or update your page.

Now, the 5 latest posts will be displayed on the specified page.

Note: If you’re using a page template or a custom PHP file to display the latest posts, you can insert the code snippet directly into the template file instead of using the Custom HTML block or the Text tab in the Classic Editor.

Remember to always backup your files before making any changes, and test your changes on a staging site or local development environment before applying them to your live site.

Leave a Reply

Your email address will not be published. Required fields are marked *