Solution:2
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<ul>
<?php
$the_query = new WP_Query( 'posts_per_page=5' ); ?>
<?php
while ( $the_query -> have_posts()) : $the_query -> the_post();
?>
<li><a href= "<?php the_permalink() ?>" ><?php the_title(); ?></a></li>
<li><?php
the_excerpt(__( '(more…)' )); ?></li>
<?php
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.