Shortcode to Show Current Posts on Any Submit or Web page

Shortcode to Show Current Posts on Any Submit or Web page


WordPress / All 65 Views comments

WordPress offers a widget that can be utilized to show current posts in any sidebar or widgetized location. Likewise many WordPress themes present some type of "current submit" performance, so customers can show their newest posts in particular places across the theme. Such performance is nice and helpful for displaying current posts nearly anyplace in your theme. Drawback is, these strategies do not work for displaying current posts inside posts, pages, and customized submit varieties. Like inside submit content material itself. For that, we will use a shortcode.

Shortcode to the rescue

To be able to show an inventory of associated posts from inside the WordPress submit editor (e.g., RTE/Visal/TinyMCE or Plain-Textual content Editor), you need to use the built-in WordPress widget or a plugin, however that could be overkill relying in your website technique, objectives, and so forth. By including the next fairly easy slice of code your theme's features.php file:

// current posts shortcode
// @
perform shapeSpace_recent_posts_shortcode($atts, $content material = null) 
	international $publish;
		'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 $publish) 
		$output .= '<li><a href="'. get_the_permalink() .'">'. get_the_title() .'</a></li>';
	return '<ul>'. $output .'</ul>';

add_shortcode('recent_posts', 'shapeSpace_recent_posts_shortcode');

As is, this code creates a shortcode that will get a customizable set of posts from the WordPress database, and shows them in your publish or web page. So simple as "one, two three", as they are saying. No modifications are required, this common "current posts" shortcode may be added to any WordPress theme, or alternately the code may be added to your website by way of easy plugin.


To make use of the recent-posts shortcode, add the next to any WP publish or web page:

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

That may show an inventory of 5 posts from class with ID = 7. You possibly can customise the attributes nevertheless is desired. The shortcode additionally accepts a few different attributes, order and orderby:

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

So now the listing will embrace 10 posts from any class, ordered randomly and displayed in ascending order.

Code rationalization

This recent-posts method combines the WP add_shortcode() function with the get_posts() template tag. Primarily outline all of the arguments, faucet the database by way of get_posts(), after which output the leads to HTML listing format. And naturally hook every part into WordPress by way of the Shortcode API. So it is truly very normal stuff, and since get_posts() makes use of the identical parameters as WP_Query, you are able to do far more when it comes to customizing and querying extremely particular units of posts. Take a look at the WP_Query docs for extra concepts.