More advanced WordPress users may want to add recent posts directly in their WordPress theme files. Of course, you should use a child theme for this so that when you update your theme, you don’t lose your changes.
It’s always a good idea to create a backup before you edit your theme files. If anything does go wrong, you might want to take a look at our list of the most common WordPress errors and how to fix them.
The easiest way to manually display recent posts to use the built-in WP_Query class. Simply add this code where you want to display the recent posts.
<ul> <?php // Define our WP Query Parameters $the_query = new WP_Query( 'posts_per_page=5' ); ?> <?php // Start our WP Query while ($the_query -> have_posts()) : $the_query -> the_post(); // Display the Post Title with Hyperlink ?> <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li> <li><?php // Display the Post Excerpt the_excerpt(__('(more…)')); ?></li> <?php // Repeat the process and reset once it hits the limit endwhile; wp_reset_postdata(); ?> </ul>
This code displays the five most recent posts with their title and excerpt. The WP_Query class has tons of parameters that allow you to customize it any way that you like. For more information please refer to the WordPress developer documentation.
« How to add WordPress Related Posts Without Plugins
How to Apply jQuery UI Datepicker in WordPress »