【问题标题】:delete row from ACF repeater field从 ACF 转发器字段中删除行
【发布时间】:2019-03-19 05:24:25
【问题描述】:

我有一个按钮,当您单击它时,它会触发对函数的 ajax 调用,该函数应根据数据属性删除 acf 转发器字段中的一行。

这是函数,但有问题的行不会删除:

add_action('wp_ajax_nopriv_remove_from_playlist','remove_from_playlist');
add_action( 'wp_ajax_remove_from_playlist', 'remove_from_playlist' );

function remove_from_playlist() {
    $field_key = 'field_5b6b8c16eb66b';
    $userID = $_POST['user'];
    $lessonID = $_POST['lessonID'];

    if( have_rows('user_playlist', $userID) ):
        while( have_rows('user_playlist', $userID) ) : the_row();
            $value = get_sub_field('post_id');

            if($value === $lessonID) {
                $row = get_row_index();
                delete_row($field_key, $row, $userID);
            }
        endwhile;
    endif;
    die();
}

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    这没有很好的记录;您需要告诉 ACF 您传递的是用户 ID,而不是帖子 ID。为此,请将user_ 附加到您的变量中:

    if( have_rows('user_playlist', 'user_' . $userID) ):
        while( have_rows('user_playlist', 'user_' . $userID) ) : the_row();
            $value = get_sub_field('post_id');
    
            if($value === $lessonID) {
                $row = get_row_index();
                delete_row($field_key, $row, 'user_' . $userID);
            }
        endwhile;
    endif;
    

    【讨论】:

    • 对不起,我应该更清楚...所以 $userID 变量中已经包含 user_ 。所以实际的 $userID 值是 user_1,因为我是以管理员身份登录的。
    • 以上代码有效,因此您的 Ajax 调用或 POST 参数一定是导致问题的原因。
    猜你喜欢
    • 2020-05-15
    • 2021-04-26
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 2014-07-04
    • 2018-04-12
    • 2019-05-24
    • 2014-07-07
    相关资源
    最近更新 更多