首页 WordPress函数大全 update_sub_row()

update_sub_row()

2020-06-28 / 1310阅 / 悠然

如何你看完本文还不知道如何使用该函数,可以联系我定制视频教程,50元/个函数,学不会全额退款

本视频需要登录以后才能播放

登录

总览

更新现有Repeater或Flexible Content子字段值的数据行。

此功能可以在have_rows()循环内部或外部使用。在内部使用时,当前行将用于更新子字段值。在外部使用时,必须指定行和父对象以定位正确的值位置。

请注意,此功能用于修改子字段值。要修改父字段值,请使用update_row()函数。

参量

update_sub_row($selector, $row, $value, [$post_id]) 
  • $selector (字符串|数组) (必需) 子字段名称或键,或祖先和行号的数组。
  • $row (int) (必需) 要更新的行号。
  • $value (数组) (必需) 新行数据。
  • $post_id (混合) (可选) 保存值的帖子ID。默认为当前帖子。

返回

(布尔)成功更新为true,失败更新为false。

变更记录

  • 在版本5.4.7中添加

更新have_rows()循环内的子行

本示例说明如何将数据行更新到称为“ images”的现有嵌套转发器字段。此嵌套的转发器字段包含3个子字段(“图像”,“ alt”,“链接”)。

// Loop over all staff members.
if( have_rows('staff_members') ) {
    while( have_rows('staff_members') ) {
        the_row();

        // Get this staff member's name.
        $name = get_sub_field('name');

        // Update this staff member's first image.
        $value = array(
            'image' => 123,
            'alt'   => "The first photo of $name",
            'link'  => 'http://website.com'
        );
        update_sub_row('images', 1, $value);
    }
} 

更新have_rows()循环外的子行

本示例说明了如何在have_rows()循环外更新一行数据。在此,为$selector参数提供了一个包含字段名和行号混合的数组。该数组应从左到右读取,父子关系由行号分隔。

请参阅有关索引偏移量的注释。

// Update the first staff member's first image.
$value = array(
    'image' => 123,
    'alt'   => "The first photo of the first staff member",
    'link'  => 'http://website.com'
);
update_sub_row( array('staff_members', 1, 'images'), 1, $value );

// Update the first staff member's second image.
update_sub_row( array('staff_members', 1, 'images'), 2, $value ); 

笔记

索引偏移

当使用特定的行号作为子字段的目标时,请注意,行号从1开始而不是0。这意味着第一行的索引为1,第二行的索引为2,依此类推。

要从0开始索引,请像这样使用row_index_offset设置。

functions.php

add_filter('acf/settings/row_index_offset', '__return_zero'); 
大家谈论
    我的见解