【问题标题】:WordPress ACF - Required field based on other fields imputWordPress ACF - 基于其他字段输入的必填字段
【发布时间】:2019-03-03 23:00:55
【问题描述】:

我在我的网站上使用 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": ""
    }
]

它不起作用..它给了我一个结果,其中标题字段始终是必需的。

希望有人可以提供帮助。提前谢谢!

附言。我知道如何使用条件逻辑隐藏字段,但这不会清空字段,因此仍然可以在网站上看到。因此,另一种解决方案是在 Title 为空时清空字段。

【问题讨论】:

    标签: json wordpress advanced-custom-fields


    【解决方案1】:

    您可以在要隐藏前端的字段周围使用 if else 语句吗?浏览器

    <?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 字段,该字段可以使用条件逻辑来隐藏字段后端,并在模板页面上添加一个更简单的 if else。

    <?php if( get_field('new_true_false_field') ): ?>
        <p>My field value: <?php the_field('title'); ?></p>
    <?php endif; ?> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 2013-07-19
      • 2020-09-20
      • 1970-01-01
      • 2019-07-20
      相关资源
      最近更新 更多