This is my data (example):
var obj = {
something:'custom'
people:[
{
name:'john',
age:51
},
{
name:'jenny',
age:62
}
]
}
And I use such shortcode:
[foo name="john,jenny" age="51,62" something="custom"]
Let say I need to have multiple objects like that in one shortcode, how would I make the shortcode look like, so I can process it and get data?
This is my data (example):
var obj = {
something:'custom'
people:[
{
name:'john',
age:51
},
{
name:'jenny',
age:62
}
]
}
And I use such shortcode:
[foo name="john,jenny" age="51,62" something="custom"]
Let say I need to have multiple objects like that in one shortcode, how would I make the shortcode look like, so I can process it and get data?
Share Improve this question asked Dec 4, 2019 at 0:14 ToniqToniq 4476 silver badges15 bronze badges 2- Are you trying to use the shortcode to get data from the object? Or put data into an object? What are you trying to achieve here? – Jacob Peattie Commented Dec 4, 2019 at 0:32
- I am just wonering how the shortcode can be formatted, what is allowed? – Toniq Commented Dec 4, 2019 at 1:25
1 Answer
Reset to default 0you can make a string and parse it. you start with
[foo people="name:john,age:51|name:jenny,age:62" something="custom"]
and you use this code
add_shortcode("foo", function ($attr, $content, $tag) {
// parsing attributes
$attr["people"] = explode("|", $attr["people"]);
$attr["people"] = array_map(function ($e) {
$tab = [];
foreach (explode(",", $e) as $raw_tab) {
$tab2 = explode(":", $raw_tab);
$tab[$tab2[0]] = $tab2[1];
}
return $tab;
}, $attr["people"]);
/*
$attr = array(
'people' => array(
0 => array(
'name' => 'john',
'age' => '51',
),
1 => array(
'name' => 'jenny',
'age' => '62',
),
),
'something' => 'custom',
);
*/
// generate output
$result = "...";
return $result;
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744942302a4602401.html
评论列表(0条)