WordPress Fetching Latest 5 Post using WP_Query()

WordPress Fetching Latest 5 Post using WP_Query()

This post  is all about how to fetch WordPress’s latest 5 post in descending order suing WP_Query(). As we know WordPress is now world best blogging tool and CMS, extremely used for creating Website. Sometime when we are working on custom template and want to so show worpress lates 5 / 3 / 4 post in block we can use wordpress’s function WP_Query(), code is given block and I don’t think its need any explanation but somehow you find it difficult to understand then please comment, I will explain through comment’s reply.

Code for Fetching WordPress Latest 5 post in Descending order

<?php
$query = new WP_Query(array('post_type'=>'post','posts_per_page=>5','orderby' => array( 'id' => 'ASC' )));
while($query->have_posts()): $query->the_post();
?>

<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="post-entry">
<a href="<?php echo get_permalink($post->ID);?>">
<?php
if(has_post_thumbnail()){
the_post_thumbnail( 'thumbnail', array(
'class' => 'img-full'
));
}else{
echo '<img src="'.get_template_directory_uri().'/img/thumb.png" class="img-full" alt="No Image">';
}
?>
</a>
<h2> <a href="<?php echo get_permalink($post->ID);?>"><?php the_title();?></a> </h2>
<ul class="entry-meta">
<li> <i class="fa fa-user"></i> <?php the_author(); ?> </li>
<li><i class="fa fa-calendar"></i> <?php the_date(); ?></li>
</ul>
<p>
<?php echo the_excerpt(); ?>
</p>
<a class="btn btn-primary" href="<?php echo get_permalink($post->ID);?>">Read More </a>
</div>
</div>

<?php
endwhile;
?>

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do you have any Query / Suggestion