admin管理员组文章数量:1311461
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!
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!
Share Improve this question edited Jan 13, 2021 at 14:08 Knott asked Jan 13, 2021 at 10:41 KnottKnott 4542 gold badges7 silver badges27 bronze badges 4 |1 Answer
Reset to default 1The 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 here — author,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.. =)
本文标签: theme customizerOutput foreach loop used in Wordpress wpcustomize
版权声明:本文标题:theme customizer - Output foreach loop used in Wordpress wp_customize 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741833139a2400055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$list = explode( ',', $box );
then loop through the array. – Sally CJ Commented Jan 13, 2021 at 15:29$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 Commented Jan 15, 2021 at 8:22