2

我的代码在redux中如下

$fields = array(
    'id'       => 'opt-select',
    'type'     => 'select',
    'title'    => __('Select Option', 'redux-framework-demo'), 
    'subtitle' => __('No validation can be done on this field type', 'redux-framework-demo'),
    'desc'     => __('This is the description field, again good for additional info.', 'redux-framework-demo'),
    // Must provide key => value pairs for select options
    'options'  => array(
        '1' => 'Opt 1',
        '2' => 'Opt 2',
        '3' => 'Opt 3'
    ),
    'default'  => '2',
);

现在,如果设置了标题变体 2,我想显示第 3 个选项。标头变体是另一个字段,它有两个选项,例如标头变体 1 和标头变体 2。如何编写代码来实现此功能?

4

1 回答 1

0

您可以对任何给定字段使用 Redux Framework 的required参数。

文档

可以根据一个/多个父值链接/必需/折叠字段。这是通过在任何给定字段上附加一个必需的参数来完成的,类似于以下内容:

'required' => array('opt-header-variations','equals','2')
  • 数组的第一个值是将字段链接到的字段 ID。
  • 第二个值是要执行的操作。
  • 第三个值是要比较的值。

required参数支持一些运算符,因此您可以执行一些逻辑。


一个真实的例子:

下面我们设置两个字段,都是选择字段类型,但对于任何字段类型都一样。我们希望隐藏带有“opt-select” id 的选择字段,并且仅在选择字段“opt-header-variations”值等于时才显示它2,在这种情况下与“Header variant 2”选项相同。

为了做到这一点,我们需要在required我们想要有条件地隐藏的选择字段上使用参数:

'required' => array('opt-header-variations','equals','2'),

$fields = array(
    array (
        'id' => 'opt-header-variations',
        'type' => 'select',
        'title' => __('Header Styles', 'redux-framework-demo'),
        'subtitle' => __('Header Variations', 'redux-framework-demo'),
        'options'  => array(
            '1' => 'Header variation 1',
            '2' => 'Header variation 2'
            ),
        'default' => 1,
    ),
    array (
        'id'       => 'opt-select',
        'type'     => 'select',
        'title'    => __('Select Option', 'redux-framework-demo'), 
        'subtitle' => __('No validation can be done on this field type', 'redux-framework-demo'),
        'desc'     => __('This is the description field, again good for additional info.', 'redux-framework-demo'),
         // Must provide key => value pairs for select options
        'options'  => array(
            '1' => 'Opt 1',
            '2' => 'Opt 2',
            '3' => 'Opt 3'
            ),
        'default'  => '2',
        'required' => array('opt-header-variations','equals','2')
    ),
);

required参数还可以与多个“父”必需值一起使用。如果不满足所有这些条件,则该字段将不可见,并且不会使用输出 CSS。一个例子如下:

'required' => array( 
    array('layout','equals','1'), 
    array('parent','!=','Testing') 
)
于 2017-02-14T04:31:05.430 回答