您可以使用 set_conditional_logic( $rules )
将条件逻辑应用于字段,以根据同一容器中的其他字段显示或隐藏它。 语法类似于 meta_query
。
条件规则在二维数组中传递,每个规则可以具有以下参数作为key=>value
键名 | 描述 | 类型 | 默认值 | 必填 |
---|---|---|---|---|
field |
应用规则的字段名称。名称应与容器中定义的名称相同。 | string |
— | 是的 |
value |
字段的值。仅当 为 、 或 时,它才可以是数组。compare IN NOT IN INCLUDES EXCLUDES |
string 或array |
"" |
不 |
compare |
要测试的操作员。可能的值为:、 、 、 、 、 、 。= < > <= >= IN NOT IN INCLUDES EXCLUDES |
string |
= |
不 |
您可以选择传递密钥并将其设置为(默认)或 . 它定义了关系,当有多个规则时。relation
AND
OR
例子:
Field::make( 'select', 'crb_show_socials', 'Show Socials' )
->add_options( array(
'yes' => 'Yes',
'no' => 'No',
) ),
Field::make( 'text', 'crb_facebook', 'Facebook URL' )
->set_conditional_logic( array(
'relation' => 'AND', // Optional, defaults to "AND"
array(
'field' => 'crb_show_socials',
'value' => 'yes', // Optional, defaults to "". Should be an array if "IN" or "NOT IN" operators are used.
'compare' => '=', // Optional, defaults to "=". Available operators: =, <, >, <=, >=, IN, NOT IN
)
) ),
注意:条件逻辑字段的范围仅限于当前字段的兄弟。
为了依赖父字段,请在其名称前加上 . 例如,要依赖向上 2 级的字段,请使用
parent.crb_in_production
parent.parent.crb_in_production
例子:
Field::make( 'checkbox', 'crb_in_production', 'In Production' ),
Field::make( 'complex', 'crb_makes', 'Makes' )
->add_fields( array(
Field::make( 'complex', 'models', 'Models' )
->add_fields( array(
Field::make( 'text', 'name', 'Name' ),
Field::make( 'text', 'price', 'Price' )
->set_conditional_logic( array(
array(
'field' => 'parent.parent.crb_in_production',
'value' => true,
)
) )
) )
) ),