在主题定制器上将两个Active_Callback合并为一个

时间:2017-05-30 作者:Gregory Schultz

我正在使用主题定制器,试图找到一种方法来检查两个设置。下面是我的代码,并解释了它的作用:

// start a setting called \'twsa_show_active_days\'
$wp_customize->add_setting( \'twsa_show_active_days\' );
// add options to the setting, in this case a radio to select three options
$wp_customize->add_control( \'twsa_show_active_days\', array(
\'description\' => \'Weekdays and/or weekends?\',
\'type\' => \'radio\',
\'section\' => \'twsa_show\',
\'choices\' => array(
    \'weekdays\' => \'Weekdays\',
    \'plus_sat\' => \'Weekdays plus Saturday\',
    \'full_week\' => \'Weekdays plus Saturday & Sunday\'),
// active callback to check if \'active\' is selected in another setting
\'active_callback\' => function() use ( $wp_customize ) {
    return \'active\' === $wp_customize->get_setting( \'twsa_show_schedule\' )->value();
},
// another active_callback to check if \'twsa_show_active\' is selected in another setting
\'active_callback\' => function() use ( $wp_customize ) {
    return \'daily\' === $wp_customize->get_setting( \'twsa_show_active\' )->value();
}) );
我的问题是,你如何使用active_callback 要检查两个设置?它在一个设置下工作,但我需要它来检查两个设置。是否需要创建函数?

2 个回复
最合适的回答,由SO网友:cjbj 整理而成

也许我误读了你的问题,但在一个匿名函数中结合这两个条件看起来并不难:

\'active_callback\' => function() use ( $wp_customize ) {
    $condition1 == $wp_customize->get_setting( \'twsa_show_schedule\' )->value();
    $condition2 == $wp_customize->get_setting( \'twsa_show_active\' )->value();
    return ($condition1 && $condition2); 
    }
对于主动回调的真正聪明思考,read Otto\'s take on them.

SO网友:Mark Kaplun

一般来说,对签入的内容不应有任何限制active_callback 或者代码是如何编写的。同时检查两个(或多个)设置不应有任何限制。

结束

相关推荐

Functions are causing errors

我在插件的PHP上声明一个函数。插件在每个帖子的顶部加载代码(帖子布局是插件的名称)。无法在/home/content/08/10290908/html/wp-content/plugins/post-layout/plugin.php(181):eval()\'d code:4)中重新声明vote\\u-up()(之前在/home/content/08/102908/html/wp-content/plugins/post-layout/plugin.php中声明)。php(181):第36行的eval