Create Shortcodes to Display Recent Posts With Parameters

To display recent posts with parameters using a shortcode without using a plugin, you will have to create a custom shortcode with parameters.

I have written a separate article on creating custom shortcodes, where I have outlined passing parameters and attributes in custom shortcodes.

Now to create a shortcode to display recent posts, copy the below code snippet in your theme’s functions.php file.

// Creating Shortcodes with parameter to display recent posts

function diwp_shortcode_display_post($attr, $content = null){

	//call global variable
        global $post;

	// Defines Shortcode's Attributes
	$shortcode_args = shortcode_atts(
					  array(
						'cat'	 => '',
						'num'	 => '3',
						'order'  => 'desc'
					    ), $attr);	
	
	// set an array with query arguments
	$args = array(
			'cat'		 => $shortcode_args['cat'],
			'posts_per_page' => $shortcode_args['num'],
			'order'          => $shortcode_args['order']
		     );
	
	// get posts  
	$recent_posts = get_posts($args);

	$output = '
<ul>';
	
	// extracting each post by foreach loop
	foreach ($recent_posts as $post) :
		
		//  set new post data
		setup_postdata($post);

		$output .= '
<li><a href="'.get_permalink().'">'.get_the_title().'<a></li>

';	


	endforeach;	
	
	// reset post data  
	wp_reset_postdata();

	$output .= '</ul>

';
	
	//return the output.
	return $output;

}

// Register the shortcode.
add_shortcode( 'diwp_recent_posts', 'diwp_shortcode_display_post' );

The above code will create a custom shortcode called [diwp_recent_posts] with the attribute cat, num, and order.

  • cat: pass category id
  • num: number of posts to display
  • order: order of posts to display: asc | desc

You can use these attributes with your shortcode to get desired results.

For example: Add the below shortcode to your page, post, or widget to display recent posts. this shortcode will give you the list of 10 recent posts in ascending order.

[diwp_recent_posts num="10" order="asc"]

If you want to add this shortcode into your theme file or with PHP, then use it with do_shortcode() function as given below.

<?php do_shortcode('[diwp_recent_posts num="10" order="asc"]'); ?>

Let’s understand the example:

In the above snippets, I have created a callback function for shortcode and registered that shortcode. So let’s understand what is going on in my callback function.

  1. I have created a callback function and passed ( $attr, $content ) parameters in it.
    function diwp_shortcode_display_post($attr, $content = null)
    
  2. Then here, I have called WordPress’s global function for post.
    //call global variable
    global $post;
    
  3. In the next step, I have defined the attributes for my shortcode.
    $shortcode_args = shortcode_atts(
    			    array(
    				  'cat'	 => '',
    				  'num'	 => '3',
    				  'order'  => 'desc'
    			     ), $attr);
    
  4. Next, I have created an array with the arguments that will be passed in the posts query.
    // set an array with query arguments
    	$args = array(
    			'cat'		 => $shortcode_args['cat'],
    			'posts_per_page' => $shortcode_args['num'],
    			'order'          => $shortcode_args['order']
    		     );
    
    
  5. In the next step, have called a template tag or function get_posts($args); to retrieve all the posts as per my arguments.
    $recent_posts = get_posts($args);

Hope this article helps you to learn about creating shortcodes to display recent posts with shortcodes parameters.