0

I'm trying to dynamically output some settings from theme Customizer. I use this custom controls, Example 3.

How can I output the settings so that the selected options appear as well as in the order in which they were placed in Customizer?

So far I've tried without success:

    $box = get_theme_mod( 'sample_pill_checkbox3' ) ;
 
    switch ( $box ) {

                case 'author':
                echo 'Author';
                break;

                case 'date':
                echo 'Date' ;
                break;
}

Basically, if Author and Date is selected and Date is placed first, so I would need to appear in the frontend, Date first then Author.

Thank you!

4
  • 1
    What you're asking is actually a generic PHP question.. and looking at the code, you'd want to do $list = explode( ',', $box ); then loop through the array. – Sally CJ Jan 13 at 15:29
  • @SallyCJ I'm not that good at php. What should my code look like? Can you formulate an answer please? – Knott Jan 15 at 6:57
  • 1
    Try with $list = explode( ',', $box ); foreach ( $list as $value ) { switch ( $value ) { // your code here } } - and if it works the way you expected, then you can write your own answer (and accept it later). – Sally CJ Jan 15 at 8:22
  • 1
    @SallyCJ Brilliant! Work like charm! You can add your answer, please! – Knott Jan 15 at 14:15
1

The code you have in question (switch ( $box )) won't work because the value of the $box variable is in the form of <value>,<value>,<value>,..., i.e. comma-separated list of values (like the default ones hereauthor,categories,comments), so in order to get access to each value in that list, you'd want to parse the values into an array, e.g. using the native explode() function in PHP, just like how the theme author did it.

Then after that, just loop through the array and run the switch call for each item in the array. (Note that the items are already in the same order they were placed via the Customizer)

Working Example

$list = explode( ',', $box );

foreach ( $list as $value ) {
    switch ( $value ) {
        case 'author':
            echo 'Author';
            break;

        case 'date':
            echo 'Date';
            break;

        // ... your code.
    }
}

PS: Just a gentle reminder — if you need a generic PHP help like this again, you should ask on Stack Overflow.. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.