Add recent posts to articles (with and without plugins)

October 30, 2019 313 views
WordPress

Friends, please help. On the site we use the lite version of Astra theme.

Task: add recent posts to articles. Now they are displayed simply, as the next and previous.

I plan to use the plugin for the solution, but find it difficult to choose:

  1. https://wordpress.org/plugins/smart-recent-posts-widget/
  2. https://wordpress.org/plugins/display-posts-shortcode/
  3. https://wordpress.org/plugins/content-views-query-and-display-post-page/

What do you think about my choice? Can you recommend something?

It is necessary:
– ease of code
– adaptability
– performance

Perhaps there is an option to do without a plugin?

1 Answer

Hello,

I think that the easiest way of doing so is to use the WordPress Recent Posts Widget and add your recent posts to your sidebar for example.

I would not really use a plugin as WordPress comes with a built-in widget to display recent posts anyway. To enable that inside your WordPress admin, visit Appearance > Widgets and add Recent Posts widget to a sidebar.

If you prefer to d this with code, you could use something like this and add it to your theme files:

<ul>
// Define your WP Query Parameters
<?php $the_query = new WP_Query( 'posts_per_page=5' ); ?>

// Prepare your WP Query
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

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

// Get 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>

Hope that this helps!
Regards,
Bobby

Have another answer? Share your knowledge.