Display Recent Posts in WordPress Home Page only


If you need to display your recent posts in the front page of your blog and nowhere else, you don’t need a dedicated plugin, although there is one you can use; Widget Options lets you have a widget only in the home page.

Here is my code; feel free to copy, modify and use it to show your recent 5 posts. Changing this number you can show less or even more:

<?php
if (is_front_page() || is_home()) {
echo '<h3>Write Here the Title for your Recent Posts list</h3><div class="AnyStyleYouLike"><ul>';
$query = new WP_Query(array(
'posts_per_page' => 5,
));
while ($query->have_posts()): $query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; 
echo '</ul></div>';
}
?>

Enjoy!

    Comments are closed