Create Shortcodes to Display Recent Posts With Parameters

If you want to know, how to create shortcodes to display recent posts without plugin then this article helps you to learn.

To create shortcodes in WordPress, you should be familiar with how to create shortcodes and type of shortcodes in WordPress. Now let’s create a shortcode to display recent posts. To create shortcode 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' );

Add the below shortcode and in your page, post or widget to display recent posts.
Usage:

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

If you want to add this shortcode in your theme file or with PHP, then use the code given below.

<?php [diwp_recent_posts num="10" order="asc"] ?>

This will give you a list of recent posts. This shortcode has num and order, parameters, so you can limit the number of posts and set order of your posts to display.

Let’s understand by 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 twpg_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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top