2016-10-04 63 views
2

我的代碼是如下面終極版現在如何根據其他字段值更改選擇字段類型的重疊選項?

$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設定將被顯示。標題變化是另一個有兩個選項的字段,例如標題變化1和標題變化2. 如何編寫代碼以實現此功能?

回答

0

對於任何給定的字段,您可以使用Redux Framework的required參數。

Docs

字段可以被鏈接/需要/根據一個/多個父 值(S)摺疊。這是通過附加一個必需的參數來完成, 類似於以下,對任何給定的字段:

'required' => array('opt-header-variations','equals','2') 
  • 陣列的第一個值是在其中的字段鏈接字段ID。
  • 第二個值是要執行的操作。
  • 第三個值是要比較的值。

required參數支持一些操作符,因此您可以執行一些邏輯操作。


一個真實的例子:

婁我們設置兩個領域,都是選擇字段類型,但它適用於任何字段類型相同。我們想要隱藏選擇字段「opt-select」 id,並且只有在選擇字段「opt-header-variations」值等於2時才顯示,在這種情況下與「Header variation 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') 
) 
相關問題