Create custom shortcode with attributes in WordPress

add_shortcode( 'shortcode_with_atts', 'create_shortcode_with_atts' );
function create_shortcode_with_atts( $atts ) {

	$attsVal = shortcode_atts( array(
		'posts_per_page'  =>  '2',
		'post_category'  =>  'all',   
	), $atts );

	//your custom code starts
	//access attribute values like this
	// $attsVal['pagination']; 
	// $attsVal['posts_per_page'];
	// $attsVal['post_category']; 
	//your custom code ends

	return  ob_get_clean();

How use this shortcode in frontend?

[shortcode_with_atts posts_per_page="10" post_category="news"]
Need a helping hand in fixing your website issues?

If you are facing any problems in implementing these code snippets and tutorials, you can hire us to fix your website issues.

Hire Us