【问题标题】:Multiple ACF date-conditions via php array通过 php 数组的多个 ACF 日期条件
【发布时间】:2020-10-13 06:39:21
【问题描述】:

是的,我还在尝试,不,我还没有找到答案。

我在 ACF 表单上发布过多次,我什至创建了官方 ACF 票证,我在 Stack 上多次发布过...

真的不明白为什么人们不愿意在几行代码上帮助我,但我想这就是它的工作方式:(

我的最后一次尝试,然后我不得不退出。不是因为我想要,而是因为我必须这样做,因为缺少完成拼图的最后一个关键。

如果我通过 PayPal(预先)向您汇款 75 美元,您会帮助我吗?

这个工作代码:

// Apply conditions to fields
add_filter('acf/prepare_field/name=booking_time_session_1', 'yl_check_booking_setting_exceptions');
function yl_check_booking_setting_exceptions($field){

    if ( have_rows('booking_setting_exceptions', 'booking_settings') ) {
        while ( have_rows('booking_setting_exceptions', 'booking_settings') ) {
            the_row();
            
            if (get_sub_field('booking_setting_exceptions_session', 'booking_settings') == '1' ) {
                $date = date_i18n('Ymd', strtotime(get_sub_field('booking_setting_exceptions_date', 'booking_settings')));

                // Bail early if no option date found
                if (empty($date)) {
                    return $field;
                }

                // Add the condition to the field
                $field['conditional_logic'] = array(

                    array(

                        array(
                            'field'     => 'field_5ed4181bd63dc', // Time field session 1 in the form
                            'operator'  => '==', // If Value is different, then show the field
                            'value'     => '1', // Compare against session option page value
                        ),

                        array(
                            'field'     => 'field_5ed4178dd63d7', // Time field session 1 in the form
                            'operator'  => '!=', // If Value is different, then show the field
                            'value'     => $date, // Compare against date option page value
                        )

                    )

                );
            }

        }

    }

    // Return
    return $field;

}

给我这个数据条件输出:

data-conditions="[[{"field":"field_5ed4181bd63dc","operator":"==","value":"1"},{"field":"field_5ed4178dd63d7","operator":"!=","value":"20200625"}]]"

20200625 是转发器字段中 3 行的最后日期(行)。

我只是想要这部分 [{"field":"field_5ed4181bd63dc","operator":"==","value":"1"},{"field":"field_5ed4178dd63d7","operator":"!=","value":"20200625"}] 多次以逗号分隔,因此所有条件都将处于活动状态。

我可以雇用你吗?了解 ACF 的人应该立即解决这个问题 :)

如果没有...我周围的其他人可以为此雇用吗?

【问题讨论】:

    标签: php arrays field advanced-custom-fields


    【解决方案1】:

    不确定您在这里期待什么,SO 不是编码服务。

    但无论如何,在我看来你需要推动你的条件,现在你正在覆盖它们 - 我在这里做了一个小操场:https://www.tehplayground.com/YIO6KLfKfxVHFYRQ 看起来格式正确的地方。

    <?php 
    // Apply conditions to fields
    add_filter('acf/prepare_field/name=booking_time_session_1', 'yl_check_booking_setting_exceptions');
    function yl_check_booking_setting_exceptions($field){
    
    if ( have_rows('booking_setting_exceptions', 'booking_settings') ) {
        //init the conditions array - When conditions are set, it need to be in array(array(array('conditions'))); format
        $field['conditional_logic'][0] = array();
        
        while ( have_rows('booking_setting_exceptions', 'booking_settings') ) {
            the_row();
            
            if (get_sub_field('booking_setting_exceptions_session', 'booking_settings') == '1' ) {
                $date = date_i18n('Ymd', strtotime(get_sub_field('booking_setting_exceptions_date', 'booking_settings')));
    
                // Bail early if no option date found
                if (empty($date)) {
                    //set the conditions back to default 0
                    $field['conditional_logic'] = 0;
                    return $field;
                }
    
                // Add the condition to the field
                $field['conditional_logic'][0][] = array(
                            'field'     => 'field_5ed4181bd63dc', // Time field session 1 in the form
                            'operator'  => '==', // If Value is different, then show the field
                            'value'     => '1', // Compare against session option page value
                        );
                        
                $field['conditional_logic'][0][] = array(
                            'field'     => 'field_5ed4178dd63d7', // Time field session 1 in the form
                            'operator'  => '!=', // If Value is different, then show the field
                            'value'     => $date, // Compare against date option page value
                        );
            }
    
        }
    
    }
    
    // Return
    return $field;
    
    }
    ?>
    

    希望对您有所帮助。

    【讨论】:

    • 如此之多,您正试图在这里帮助我,如果我们启动并运行它,它肯定是一项编码服务,所以我会按照承诺向您付款。现在他们可能只是代码中的一个小错误,因为现在没有任何条件出现。如果您想检查,它的字段 TIJD SESSIE 1 需要数据条件:olddutch.yorlinq.com/reserveren - 希望收到您的来信:)
    • 抱歉 - 不太确定网站上应该发生什么 :-D 但您可以尝试删除 $field['conditional_logic'] = 0;,因为如果您的最后一行没有日期,它将重置整个条件字段 - 我想我能帮到你的只有这个 tbh。
    • 但至少,我看到你的网站现在有不止一个日期`2020-06-24,2020-06-25,2020-06-25`
    • 我为此使用了代码片段,因此很容易检查和尝试。如果您愿意,我可以为您提供登录凭据(它是一个测试站点,当一切正常时,我们将重建实时站点)...如果是...。我如何为您提供登录凭据? (如果你想让我先付款……那很好;))
    • 您尝试过类似justanswer.com/sip/coding 的方法吗?它们可能比我更适合帮助您。老实说,我不知道从哪里开始。保留您的资金,与他们一起开始试用会员资格,并让专门的“专家”来帮助:) 祝你好运。
    猜你喜欢
    • 2019-03-14
    • 2021-03-05
    • 2022-10-13
    • 2018-09-20
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 2021-01-26
    • 2018-06-30
    相关资源
    最近更新 更多