WordPress   

Create custom shortcode with attributes in WordPress

<?php
add_shortcode( 'shortcode_with_atts', 'create_shortcode_with_atts' );
function create_shortcode_with_atts( $atts ) {
	ob_start();

	$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 Professional Support for Your Website Problems?

Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.

Hire Us