Display Recent Posts on a Static Page (With and Without Plugin)

Suppose you want to create a page where you will write a short introduction about a particular blog topic and then display the recent posts from related categories. WordPress does not provide this option by default. Today I’ll show you how you can display recent posts on a static page. You can display just a bunch of recent posts or display posts from specific categories. I will show you both ways. I will also include some plugins that you can use as an alternative to the code snippet method.

Display Recent Posts from Specific Category

Let’s say, we want to display 10 posts from a specific category in a bulleted list. We want to show the featured images (thumbnails) as well. Now to enable the shortcode, add the following code to the “functions.php” file of your theme:

function wpcat_postsbycategory() {
// the query
$the_query = new WP_Query( array( 'category_name' => 'yourcategoryslug', 'posts_per_page' => 10 ) ); 
 
// The Loop
if ( $the_query->have_posts() ) {
    $string .= '<ul class="postsbycategory widget_recent_entries">';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
            if ( has_post_thumbnail() ) {
            $string .= '<li>';
            $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';
            } else { 
            // if no featured image is found
            $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
            }
            }
    } else {
    // no posts found
}
$string .= '</ul>';
 
return $string;
 
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts', 'wpcat_postsbycategory');
 
// Enable shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');

On line 3, please replace “yourcategoryslug” with the specific category slug. You can now display the recent category posts on any WordPress post/page by inserting the following shortcode via WordPress WYSIWYG editor:

[categoryposts]

Note: You may need to style the recent posts (using CSS) so that it matches the design of your blog.

Display Recent Posts from Specific Category in Template Files

Add the following code snippet to the template file (e.g. sidebar.php, custom.php) where you want to display recent posts from a specific category:

<?php $catquery = new WP_Query( 'cat=50&posts_per_page=5' ); ?>
<ul>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<ul><li><?php the_excerpt(); ?></li>
</ul>
</li>
<?php endwhile; ?> 
</ul>
<?php wp_reset_postdata(); ?>

The above code will display post titles along with post excerpts. On line 1, please replace “50” with your own category ID number.

Display Most recent Posts on a Static Homepage or Template File

Add the following code snippet to a custom page template, create a WordPress page using the custom template and then make it the homepage by going to Settings > Reading tab.

<?php
$args = array( 'numberposts' => 10 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
	<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
	<?php the_content(); ?>
<?php endforeach; ?>

Alternatively, use the built-in WP_Query to display the latest posts. The following code snippet displays 10 latest posts with titles and excerpts:

<ul>
// Define our WP Query Parameters
<?php $the_query = new WP_Query( 'posts_per_page=10' ); ?>
 
// 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>

Display Recent Posts with Plugins

There are a quite a few free plugins available on WordPress plugin repository that offer similar options. These plugins also provide better design customization.

Video: How to Put WordPress Posts in a Page
  1. Posts in Page
  2. Recent Posts Widget Extended
  3. WP Latest Posts
  4. Advanced Recent Posts
  5. Advanced Random Posts Widget
  6. Author Recent Posts

If you liked this post, please consider sharing it with your friends:

Pinterest

Leave a Comment