我在我的网站上使用ACF插件,并且需要一种情况,其中仅当其他字段不为空时才需要某个字段。在这种情况下,不需要“标题”字段。但是,如果以下字段之一不为空,则必须输入:交易或折扣,说明,常规价格,折扣价/交易价格以及有效期至。我需要这种情况,因为要么所有字段都必须为空,要么所有字段都必须填写。
这是我到目前为止创建的json代码:
[
{
"key": "group_deal1",
"title": "Deal 1 test 2 (displayed on homepage)",
"fields": [
{
"key": "field_deal1title",
"label": "Title",
"name": "deal_title_one_test1",
"type": "text",
"instructions": "(max. 100 characters)",
"required": [
[
{
"field": "field_deal1dealdiscount",
"operator": "!=empty"
}
],
[
{
"field": "field_deal1description",
"operator": "!=empty"
}
],
[
{
"field": "field_deal1regularprice",
"operator": "!=empty"
}
],
[
{
"field": "field_deal1discountedprice",
"operator": "!=empty"
}
],
[
{
"field": "field_deal1validdate",
"operator": "!=empty"
}
]
],
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "deals-title",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": 100
},
{
"key": "field_deal1dealdiscount",
"label": "Deal or Discount",
"name": "discount_deal_or_special_one",
"type": "radio",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"choices": {
"Discount": "Discount",
"Deal": "Deal"
},
"allow_null": 1,
"other_choice": 0,
"default_value": "",
"layout": "vertical",
"return_format": "value",
"save_other_choice": 0
},
{
"key": "field_deal1description",
"label": "Description",
"name": "deal_description_one",
"type": "textarea",
"instructions": "(max. 600 characters)",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "deals-description",
"id": ""
},
"default_value": "",
"placeholder": "",
"maxlength": 600,
"rows": "",
"new_lines": ""
},
{
"key": "field_deal1regularprice",
"label": "Regular price",
"name": "regular_price_one",
"type": "number",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "deals-regular-price",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "$",
"append": "",
"min": "",
"max": "",
"step": ""
},
{
"key": "field_deal1discountedprice",
"label": "Discounted price \/ deal price",
"name": "discounted_price_one",
"type": "number",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "deals-discounted-price",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "$",
"append": "",
"min": "",
"max": "",
"step": ""
},
{
"key": "field_deal1validdate",
"label": "Valid until",
"name": "valid_until_one",
"type": "date_picker",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"display_format": "m\/d\/Y",
"return_format": "m\/d\/Y",
"first_day": 1
}
],
"location": [
[
{
"param": "post_type",
"operator": "==",
"value": "post"
},
{
"param": "post_format",
"operator": "==",
"value": "aside"
}
],
[
{
"param": "post_format",
"operator": "==",
"value": "status"
}
]
],
"menu_order": 1,
"position": "acf_after_title",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"description": ""
}
]
它不起作用。它给我的结果是始终需要标题字段。
希望有人可以提供帮助。预先谢谢你!
PS。我知道如何使用条件逻辑隐藏字段,但这不会清空字段,因此仍然可以在网站上看到。因此,另一种解决方案是在“标题”为空时清空字段。
答案 0 :(得分:0)
您可以在要隐藏前端的字段周围使用if else语句吗? IE浏览器
<?php
$dealfield1 = get_field( 'field_deal1dealdiscount' );
$dealfield2 = get_field( 'field_deal1description');
$dealfield3 = get_field( 'field_deal1regularprice');
$dealfield4 = get_field( 'field_deal1discountedprice');
if( !empty($dealfield1) || !empty($dealfield2) || !empty($dealfield3) || !empty($dealfield4) ): ?>
<h1> <?php the_field('title'); ?></h1>
<?php endif; ?>
然后,正如您在文章的最后部分中提到的那样,可以在必要时使用条件逻辑来隐藏字段。
或者您可以添加一个true的false字段,该字段可以使用条件逻辑来隐藏字段的后端,而在模板页面上则可以更简单。
<?php if( get_field('new_true_false_field') ): ?>
<p>My field value: <?php the_field('title'); ?></p>
<?php endif; ?>