使用带有IF语句的Kirki或WP定制器添加新设置

时间:2017-07-18 作者:Deka87

我有一个收音机和一个图像设置功能。php:

Spotlight_Kirki::add_field( \'spotlight_config\', array(
    \'type\'      => \'radio\',
    \'settings\'  => \'spotlight_layout_type\',
    \'label\'     => esc_html__( \'Layout Type\' , \'spotlight\' ),
    \'section\'   => \'spotlight_general\',
    \'default\'   => \'layout_dark\',
    \'priority\'  => 0,
    \'choices\'   => array(
        \'dark\'      => esc_html__( \'Dark version\', \'spotlight\' ),
        \'light\'     => esc_html__( \'Light version\', \'spotlight\' ),
        \'coloured\'  => esc_html__( \'Coloured version\', \'spotlight\' ),
    ),
) );
if ( get_theme_mod( \'spotlight_layout_type\', \'\' ) != \'coloured\' ) {

    Spotlight_Kirki::add_field( \'spotlight_config\', array(
        \'type\'      => \'image\',
        \'settings\'  => \'spotlight_bg_image\',
        \'label\'     => esc_html__( \'Background Image\' , \'spotlight\' ),
        \'section\'   => \'spotlight_general\',
        \'default\'   => esc_html__( \'\', \'spotlight\' ),
        \'priority\'  => 0
    ) );
}
如您所见,我尝试启用spotlight_bg_image 仅当前一个收音机框设置为coloured. 然而,当我选择一个时,它并没有隐藏起来。任何帮助都将不胜感激。

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

get_theme_mod 在这种情况下,只有在保存和刷新后才能工作。要基于另一个控件的值隐藏控件,可以使用active_callback 第二个控件中的参数如下:

Spotlight_Kirki::add_field( \'spotlight_config\', array(
    \'type\'      => \'radio\',
    \'settings\'  => \'spotlight_layout_type\',
    \'label\'     => esc_html__( \'Layout Type\' , \'spotlight\' ),
    \'section\'   => \'spotlight_general\',
    \'default\'   => \'layout_dark\',
    \'priority\'  => 0,
    \'choices\'   => array(
        \'dark\'     => esc_html__( \'Dark version\', \'spotlight\' ),
        \'light\'    => esc_html__( \'Light version\', \'spotlight\' ),
        \'coloured\' => esc_html__( \'Coloured version\', \'spotlight\' ),
    ),
) );
Spotlight_Kirki::add_field( \'spotlight_config\', array(
    \'type\'      => \'image\',
    \'settings\'  => \'spotlight_bg_image\',
    \'label\'     => esc_html__( \'Background Image\' , \'spotlight\' ),
    \'section\'   => \'spotlight_general\',
    \'default\'   => esc_html__( \'\', \'spotlight\' ),
    \'priority\'  => 0
    \'active_callback\' => array(
        array(
            \'setting\'  => \'spotlight_layout_type\',
            \'operator\' => \'!==\',
            \'value\'    => \'coloured\',
        ),
    ),
) );
可在此处查看该论点的文档:https://aristath.github.io/kirki/docs/arguments/active_callback.html

通常在WordPress自定义程序中active_callback 将接受一个可调用的函数/方法,您必须在其中检查所需的条件,这只是Kirki提供的一个快捷方式,使事情变得更简单。

结束

相关推荐