admin管理员组文章数量:1406942
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;
});
本文标签: Shortcode multiple values
版权声明:本文标题:Shortcode multiple values 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744942301a2633563.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论