shortcode_parse_atts
接受一个短码属性字符串,并返回键/值对的关联数组。
// UNABLE TO TEST THIS
// $value = get_post_meta(get_the_ID(), \'some_value\', true);
// $sc = $value [ \'item_ticket_tailor\' ][ 0 ];
// TEST DATA - assuming the data is a string
$sc = \'[custom_event id="3106" ][custom_ticket id="3220" show_price="true"]\';
// pad some elements for a better parse
$sc = str_replace("[", " [ ", $sc);
$sc = str_replace("]", " ] ", $sc);
// convert the string shortcode to an array
$array = shortcode_parse_atts($sc);
// the last id is the winner, so custom_event id won\'t show here
// only custom_ticket id
echo $array[ \'id\' ]; // 3220
<小时>
ALTERNATE
$sc = \'[custom_event id="3106" ][custom_ticket id="3220" show_price="true"]\';
// split up the shortcodes
$sc = str_replace("]", " ", $sc);
$sc = str_replace("[", "[", $sc);
$shorts = array_filter(explode(\'[\', $sc));
$custom_event_id = false;
// loop through each one
foreach($shorts as $short) {
// get the individual attributes of the shortcode
$atts = shortcode_parse_atts($short);
// if the first item in the parsed array is `custom_ticket` then get the id
if($atts[ 0 ] === \'custom_ticket\') {
$custom_event_id = $atts [ \'id\' ];
break;
}
}
echo $custom_event_id; // 3220