How to Display Recent Post In a Page or Post (Wordpress)



How-to-Display-All-Recent-Post


How to Display All Recent Post In a Page or Post in wordpress?


WordPress is being popular these days and adored by several developers. It's simple for articles updating  too. Do you want to show your current posts in wpExhibiting current posts helps your users locate them easily.

In this tutorial, we'll explain to you the best way to show current articles in WordPress without a plugin the manual process with the post use is recent. So that people are able to cut the external link obtain, it's almost always easier to take action without plug-in.


Displaying Recent Posts in WordPress


First thing you have to do is generate separate template as news.php and upload it in the root folder of theme. In that theme straightforward add next scripts & 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 );