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
Add a comment  | 

1 Answer 1

Reset to default 0

you 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