Display the Latest Posts on WordPress

If you are creating a custom template for your wordpress website, these codes might help you to show the latest posts of your wordpress website.


//These are the arrays that you can choose from

<?php $args = array(
	'posts_per_page'   => 5,
	'offset'           => 0,
	'category'         => '',
	'category_name'    => '', // You can specify the category name to show only the posts of this category
	'orderby'          => 'date',
	'order'            => 'DESC',
	'include'          => '',
	'exclude'          => '',
	'meta_key'         => '',
	'meta_value'       => '',
	'post_type'        => 'post',
	'post_mime_type'   => '',
	'post_parent'      => '',
	'author'	   => '',
	'post_status'      => 'publish',
	'suppress_filters' => true 
);

$latestpost = get_posts( $args ); 
foreach ($latestpost as $post) :
    setup_postdata($post);
 ?>

<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>"> /*Featured Image URL of the post*/
<?php the_title(); ?> /*Title of the post*/
<?php the_time(get_option('date_format')) ?> /*Date of the post*/
<?php the_excerpt(); ?> /*Post content excerpt*/
<a href="<?php the_permalink(); ?>" class="btn btn-danger">Read more</a> /*Link of the post*/
<?php endforeach; ?>