Like the blog? Get the book »

Shortcode to Display Recent Posts on Any Post or Page

Shortcode to Display Recent Posts on Any Post or Page

WordPress provides a widget that can be used to display recent posts in any sidebar or widgetized location. Likewise many WordPress themes provide some sort of “recent post” functionality, so users can display their latest posts in specific locations around the theme. Such functionality is great and useful for displaying recent posts just about anywhere in your theme. Problem is, those methods don’t work for displaying recent posts inside of posts, pages, and custom post types. Like inside of post content itself. For that, we can use a shortcode.

Shortcode to the rescue

In order to display a list of related posts from within the WordPress post editor (e.g., RTE/Visal/TinyMCE or Plain-Text Editor), you can use the built-in WordPress widget or a plugin, but that may be overkill depending on your site strategy, goals, and so forth. By adding the following pretty simple slice of code your theme’s functions.php file:

// recent posts shortcode
// @ https://digwp.com/2018/08/shortcode-display-recent-posts/
function shapeSpace_recent_posts_shortcode($atts, $content = null) {
	
	global $post;
	
	extract(shortcode_atts(array(
		'cat'     => '',
		'num'     => '5',
		'order'   => 'DESC',
		'orderby' => 'post_date',
	), $atts));
	
	$args = array(
		'cat'            => $cat,
		'posts_per_page' => $num,
		'order'          => $order,
		'orderby'        => $orderby,
	);
	
	$output = '';
	
	$posts = get_posts($args);
	
	foreach($posts as $post) {
		
		setup_postdata($post);
		
		$output .= '<li><a href="'. get_the_permalink() .'">'. get_the_title() .'</a></li>';
		
	}
	
	wp_reset_postdata();
	
	return '<ul>'. $output .'</ul>';
	
}
add_shortcode('recent_posts', 'shapeSpace_recent_posts_shortcode');

As is, this code creates a shortcode that gets a customizable set of posts from the WordPress database, and displays them on your post or page. As simple as “one, two three”, as they say. No modifications are required, this universal “recent posts” shortcode can be added to any WordPress theme, or alternately the code can be added to your site via simple plugin.

Usage

To use the recent-posts shortcode, add the following to any WP post or page:

[recent_posts num="5" cat="7"]

That will display a list of five posts from category with ID = 7. You can customize the attributes however is desired. The shortcode also accepts a couple of other attributes, order and orderby:

[recent_posts num="10" cat="" order="asc" orderby="rand"]

So now the list will include 10 posts from any category, ordered randomly and displayed in ascending order.

Code explanation

This recent-posts technique combines the WP add_shortcode() function with the get_posts() template tag. Essentially define all the arguments, tap the database via get_posts(), and then output the results in HTML list format. And of course hook everything into WordPress via the Shortcode API. So it’s actually very standard stuff, and because get_posts() uses the same parameters as WP_Query, you can do much more in terms of customizing and querying highly specific sets of posts. Check out the WP_Query docs for more ideas.

© 2009–2024 Digging Into WordPress Powered by WordPress Monzilla Media shapeSpace