How to get and display recent posts in WordPress?

 How to get and display recent posts in WordPress?

If you are running your own blog or a WordPress developer then this post is definitely for you. Stay with us for the next 5 mins, this post will help you to understand all about How to get and display recent posts in WordPress?.

This post discusses the detailed steps to get and display the specified number of latest posts with the respective title and thumbnails.

The code snippet can be copied and used in your WordPress site.

Every blogger always wants to show the Recent posts in WordPress blog site to attract their readers about the latest articles/posts related to the blog niche. The recent posts help bloggers to get more page view’s and thereby maintain the interest of the regular readers.

Let me explain it with an example. Suppose you have a WordPress Blog on Computer Technology Trends and you have gained a good list of readers on your blog. Then, readers who are subscribed to your blog or follow your blog may need to have an update of recent posts to learn about latest technology trends in the market.

Importance of Recent Posts:

  • Helps bloggers to get more page view’s.
  • Maintain users interest toward the blog.
  • Helps users to quickly find the latest posts/blogs/articles.
  • Helps to increase returning users for new content.
  • Helps users/people stay connected with the latest updates from the Blog.
  • Help quickly find the posts published recently.

Where to add recent posts in WordPress site?

Recent posts can be added in the following parts of your blog:

    • To Sidebar
    • At the end of the post comments sections.
    • In the footer widget areas

 

  • Inside post content with widget shortcode.

WordPress code to get recent posts:

There are different methods to get and add recent posts in WordPress site – using in-built Recent Posts Widget, Extended Plugin and manually editing WordPress theme files.

Following sample code used for Displaying Recent Posts Manually in WordPress Theme Files.
We will be using WP_Query() simplest method to query and display the specified number of recent posts.

WP_Query is a class defined in wp-includes/class-wp-query.php that deals with the intricacies of a post’s (or page’s) request to a WordPress blog. WP_Query() allows WordPress developers to create custom queries and show posts different parameters (like title, thumbnail, content, etc). So, in nutshell, it queries the posts from the WordPress database.

Just add below code snippet where you want to display the recent posts…

Get recent 5 posts with title and excerpt

#Step1 Define all the WP Query Parameters


   <!-- Define all the WP Query Parameters -->
   <?php $the_query = new WP_Query( 'posts_per_page=5' ); ?>

#Step2 – Start the query loop


  <?php

  while ($the_query -> have_posts()) : $the_query ->   
the_post(); ?>

   ........

   
   <?php
  endwhile;

#Step3 – Display the Post Title with Hyperlink


<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>

#Step4 – Display the Post Excerpt


<li><?php the_excerpt(__('(more…)')); ?></li>

Complete code:


<ul>

<!-- Define all the WP Query Parameters -->
<?php $the_query = new WP_Query( 'posts_per_page=5' ); ?>

<!-- Start our WP Query -->
<?php

while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

<!-- Display the Post Title with Hyperlink -->
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>

<!-- Display the Post Excerpt -->
<li><?php the_excerpt(__('(more…)')); ?></li>

<!-- Repeat the process and reset once it hits the limit -->
<?php
endwhile;

wp_reset_postdata();
?>
</ul>

The above sample code will display 5 latest posts with their title and excerpt.
For a complete list of parameters used in WP_Query() class, refer to this link ()

Get recent 5 posts with Title and Thumbnail:


<ul>

<!-- Define all the WP Query Parameters -->
<?php $recent_post_query = new WP_Query( 'posts_per_page=5' ); ?>

<!-- Start our WP Query -->
<?php

while ($recent_post_query -> have_posts()) : $recent_post_query -> the_post(); ?>

<!-- Display the Post Title with Hyperlink -->
<li>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>

<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail('thumbnail') ?>
<?php endif ?>

</li>

<!-- Repeat the process and reset once it hits the limit
<?php
endwhile;

wp_reset_postdata(); <!-- # reset post data so that other queries/loops work -->
?>
</ul>

We hope this article helped you. Please share your feedback in the comments section so that we can improve to provide better content in future posts.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials.

Also, visit our Facebook page.

Subscribe to our Blog to get latest posts @ TechCluesBlog

 

Leave a Reply

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