Simple Ideas To Learn How To Code
Wordpress latest post display
Wordpress

How to display all recent post in a page or post in WordPress

WordPress is being popular now a days and loved by many developers. It is easy for content updating as well. Do you want to show your recent posts in WordPress? Displaying recent posts helps your users find them easily.

In this article, we will show you how to display recent posts in WordPress without a plugin the manual method with the recent post function. It is always better to do it without plugin so that we can reduce the external connection request.

Displaying Recent Posts in WordPress

First thing you need to do is create separate template as news.php and upload it in the root folder of theme. In that template simple add following scripts and save the template.

<?php
 $recent_posts = wp_get_recent_posts();
 foreach( $recent_posts as $recent ){
 echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .  $recent["post_title"].'</a> </li> ';
 }
 wp_reset_query();
 ?>

Now, go to the page where you want to display recent post and select the template from the dropdown menu as shown below:

How to display all recent post in a page or post in WordPress

Save the file and preview it in browser, it should display all the latest posts.

To get date of post when using wp_recent_post(), the following code display latest post title as well as date in the same line.

<?php
 $recent_posts = wp_get_recent_posts();
 foreach( $recent_posts as $recent ){
 echo '<li><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"]. $recent["post_date"].'</a> </li> ';
 }
 wp_reset_query();
?>

To display the certain number of posts only

$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts( $args );

Leave a Reply

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