WordPress Shortcode to List Post With Multiple Parameters

Shortcodes are one of the most important and useful feature in WordPress. Shortcodes are a simple set of functions for creating macro codes that can be used in post/page content, widgets and anywhere else in appropriate manner. WordPress introduces the Shortcode API from version 2.5. The goal of all web developer will be to make the website as a user friendly tool. Which means without any programming knowledge/skills the user can access their website for modifying the contents and perform any alternation/changes on their own.

The most familiar and useful shortcode is to list post, page and posts from a custom post type. Here, we are going to discuss about the code snippet for creating a WordPress shortcode to list post without any plugins.

How to Create WordPress Shortcode to List Post With Multiple Parameters ?

Below is a code snippet to creates a simple shortcode, using WP_Query to run a custom query on your content and output the results as list of post. We need to add this snippet onto our theme file(function.php) after adding the code, we can use the shortcode [blog] on our website either the page, widgets or anywhere else on the wordpress.

<?php
// Shortcode to List Post based on Parameters
add_shortcode( 'blog', 'post_list' );
function post_list( $atts ) {
 ob_start();
 // Define attributes and their defaults
 extract( shortcode_atts( array (
 'type' => 'post',
 'order' => 'ASC',
 'orderby' => 'title',
 'posts' => -1,
 'category' => 'uncategorized',
 ), $atts ) );

 // Define query parameters based on attributes
 $options = array(
 'post_type' => $type,
 'order' => $order,
 'orderby' => $orderby,
 'posts_per_page' => $posts,
 'category_name' => $category,
 );
 $query = new WP_Query( $options );
 if ( $query->have_posts() ) { ?>
	<ul class="post-list">
		<?php while ( $query->have_posts() ) : $query->the_post(); ?>
		<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
		<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
		</li>
		<?php endwhile;
		wp_reset_postdata(); ?>
	</ul>
 <?php
 $myvariable = ob_get_clean();
 return $myvariable;
 }
}
?>

Using parameters onto our shortcode:

The shortcode will be much more useful if users can add their own parameters, Here are some of the parameter that you can use them on our shortcode

  • Post type, which we need to display on list post_type
  • Number of post to display on list posts_per_page
  • Ordering parameters order, orderby
  • Filter by category category_name

To add this to the shortcode, you make use of the shortcode_atts() function, which allows you to specify some default attributes which users can override.

Shortcode

Default Shortcode : [blog]
Shortcode with Parameters: [blog type="" order="" orderby="" posts="" category=""]

Thank you for reading my post. If you have any queries/feedback, Please leave your comments at below comment box.

Thank you, Once again !!

Siva Sankar

SIVA SANKAR, Working as a Software Engineer, Blogging is my hobby. I completed my Bachelors of Engineering (Computer Science Engineering) in Chennai, India. and my Master of Engineering (Embedded System Technologies) in Tamilnadu, India.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.