Parameterized Custom Shortcode

In This article I will show you How to create shortcode with Parameter in WordPress.

You can refer for the simple shortcode How To Create Custom Shortcode In WordPress

If you need any dynamic values which can be passed through the shortcode and then you need to use that in your custom code you can use this.

For example you need to get data for the different category using same shortcode so you can’t pass the static category in the shortcode custom code..

 

Shortcode: [custom_test_shortcode category=”category”]

add_shortcode('custom_test_shortcode', function ($attr) {
  $category = $attr['category'];
  $args = array(
    'post_type' 		=> 'posts',
    'category'	=> $category,
    'post_status' 		=> 'publish',
    'order' 			=> 'DESC',
    'order_by'			=> 'date'
  );
});

 

I hope this will help you.

If you find any issue you can reach out to me using comment.

Submit a Comment

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

Subscribe

Select Categories