Create WordPress Shortcode to Display Posts from Categories

In WordPress, it’s always fun to use shortcodes. They allow you to do so much with just a piece of code.

In this article, I am going to show you about creating WordPress shortcode to display recent posts from a specific category and from multiple categories.

Create Shortcode to display posts from a specific category by Category ID

To display posts from a particular category follow the steps:

  1. Copy the below code snippet and add it in functions.php file located at wp-content/themes/your-theme/.
    // Creating Shortcodes to display posts from category
    function diwp_shortcode_display_post($attr, $content = null){
    
    	global $post;
    
    	// Defining Shortcode's Attributes
    	$shortcode_args = shortcode_atts(
    						array(
    								'cat'	 => '',
    								'num'	 => '5',
    								'order'  => 'desc'
    					    ), $attr);	
    	
    	// array with query arguments
    	$args = array(
    					'cat'		 	 => $shortcode_args['cat'],
    					'posts_per_page' => $shortcode_args['num'],
    					'order'          => $shortcode_args['order'],
    					
    				 );
    
    	
    	$recent_posts = get_posts($args);
    
    	$output = '<ul>';
    
    	foreach ($recent_posts as $post) :
    		
    		setup_postdata($post);
    
    		$output .= '<li><a href="'.get_permalink().'">'.get_the_title().'<a></li>';	
    
    
    	endforeach;	
    	
    	wp_reset_postdata();
    
    	$output .= '</ul>';
    	
    	return $output;
    
    }
    
    add_shortcode( 'diwp_recent_posts', 'diwp_shortcode_display_post' );
    
    
  2. Then add this shortcode in page, post, or widgets by passing category id and other parameters in it.
    [diwp_recent_posts cat="16" num="3" order="asc"]
    
    • cat = Category id for which you want to display posts.
    • num = Number of posts to display per page.
    • order = order of posts to display, either ASC or DESC.

    If you want to add shortcode in your theme’s file or any specific template file then add it with the below code.

    <?php echo do_shortcode('[diwp_recent_posts cat="16" num="3" order="asc"]'); ?>
    
  3. This above code will display you the posts from the category which has id = “16”. Like in the image below you can see the output.
    create-shortcode-to-display-recent-posts-from-categories

So by using the above shortcode, you can easily display posts from any category. All you have to do is just pass the category id within cat attribute.

If you don’t pass category id in the shortcode or use default shortcode [diwp_recent_posts] then it will display posts from the default category (which has id = 1).

Create Shortcode to display posts from a specific category by Category Name

If you want to display posts from a category by its name instead of ID. then you can use the same function and shortcode. All you have to do is, instead of cat use category_name in the $args array like given below.

// array with query arguments
$args = array(
	'category_name'   => $shortcode_args['cat'],
	'posts_per_page'   => $shortcode_args['num'],
	'order'                    => $shortcode_args['order'],
 );

And in the shortcode, instead of category Id use category name like in the example below.

[diwp_recent_posts cat="WP Tutorials" num="3" order="asc"]

And that’s it. The above shortcode will display the posts from a category by its name. So this will display posts from the category WP Tutorials as shown in the below image.

create-shortcode-to-display-recent-posts-from-categories-2

So as of now, you have learned about display posts from a specific category but what if you want to display posts from multiple categories.

Don’t worry, I am going to cover that as well in this article.

Create Shortcode to display posts from multiple Categories

Again the above function and shortcode will be the same for displaying posts from multiple categories, but you have to make one change in your function.

// array with query arguments
	$args = array(
		'cat'	=> array($shortcode_args['cat']),
		'posts_per_page' => $shortcode_args['num'],
		'order'          => $shortcode_args['order'],
	 );

In the query arguments $args, for cat parameter, you have to pass the value as an array. So this can take multiple values.

And after doing that, add the below shortcode to display posts from different categories.

[diwp_recent_posts cat="16,17" num="4" order="desc"]

In the above shortcode, I have passed the ID of two categories as values in the cat parameter. So this will display posts from the categories, whose ids are 16 and 17.

I hope this article helped you to learn about creating shortcodes to display posts from a specific category or from multiple categories.

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