如果您有此短代码:
[product id="2"]
其重新定义如下:
add_shortcode( \'product\', \'getSingleProduct\' );
function getSingleProduct( $atts ) {
shortcode_atts( [
\'id\' => \'0\',
], $atts );
// Do someting with the "2".
do_request( \'GET\', get_api_url() . \'api/product/\' . "2" ); // This "2" comes from the shortcode
}
您可以像这样获得shortcode参数:
add_shortcode( \'product\', \'getSingleProduct\' );
function getSingleProduct( $atts ) {
// $atts is an array with the shortcode params
// shortcode_atts() function fills the array with the
// default values if they are missing
$atts = shortcode_atts( [
\'id\' => \'0\',
], $atts );
$id = $atts[\'id\'];
do_request( \'GET\', get_api_url() . \'api/product/\' . $id );
}