在 WordPress 高级自定义字段 PRO 中将行添加到嵌套重复器

问题描述 投票:0回答:4

我正在运行 ACF PRO 5.3.9.2,但我认为这适用于任何具有嵌套中继器的版本。我希望使用

update_field()
update_sub_field()
函数将行添加到嵌套转发器(即转发器内的转发器)。

update_field()
功能非常适合第一级中继器行,即:

update_field( $field_key, $value, $postID );

但我不确定如何将其用于嵌套中继器。这是 ACF 字段结构:

CUSTOM POST TYPE
    Repeater Field - "Media"
         Repeater Field - "Notes"

所以,这是我正在尝试的代码:

$field_key = "field_xxxxxxxxxxxxx"; //NOTES FIELD KEY
$value = get_field($field_key, 12); //GET FIELDS OF POST ID 12, NOTES
$value[] = array("note" => "Here is a new note");
update_field( $field_key, $value, 12 );

但这没有任何作用。无法确定我希望向哪个“媒体”转发器添加“注释”。 我可以使用

update_sub_field()

成功“更新”嵌套转发器字段,如下面的代码 IF 并且仅当嵌套转发器字段中有一行时,但它仅覆盖一行并且无法添加到其中。如果中继器中当前没有行,它也将不起作用。 update_sub_field( array('media', 1, 'notes', 2, 'note'), 'Here is a new note!', $postID );

无论是否已有行,我该如何向嵌套转发器添加行?

php wordpress advanced-custom-fields
4个回答
3
投票

获取父中继器的整个数组
  1. 循环直到找到包含要编辑的嵌套转发器的行
  2. 判断它是否是数组(如果不是,则将其设为数组)
  3. 使用
  4. array_push()
  5.  附加子中继器行
    
    使用
  6. update_field()
  7.  再次保存整个父数组
    
  8. 为了帮助说明父/子关系,请记住我有一个名为
media

的转发器字段和一个名为

notes
的嵌套转发器,其中只有 1 个子字段
note
。所以如果我使用
get_field('media')
这是它将返回的数组:

Array ( [0] => Array ( [media_title] => 'title 1', [notes] => Array ( [0] => Array ( [note] => 'HERE IS THE NOTE ADDED' ) [1] => Array ( [note] => 'Here is another note added!' ) ) ) [1] => Array ( [media_title] => 'title 2', [notes] => ) )

因此,在这个示例中,我在父中继器中有 2 个元素,其中一个在嵌套中继器中有 2 个元素,另一个在嵌套中继器中没有元素。这很重要,因为您需要确定嵌套转发器中是否已经建立了一个数组,然后才能添加它。

//LOOP THROUGH PARENT REPEATER if( have_rows('media_item', $postID) ): //SET YOUR ROW COUNTER $i = 0; //MAKE YOUR NEW ARRAY $media_item = get_field('media_item', $postID); while( have_rows('media_item', $postID) ) : the_row(); if($i == $arrayRowID) { //IF THIS IS THE ROW WE WANT TO EDIT IN PARENT REPEATER //FIND OUT IF IT IS AN ARRAY ALREADY OR NOT if( !is_array($media_item[$i]['notes']) ) { //WE DONT HAVE ANY NOTES $media_item[$i]['notes'] = array(); } $addRow = array( //SET YOUR NEW ROW HERE 'note' => 'This is my new note added!'; ); //ADD TO ARRAY array_push($media_item[$i]['notes'], $addRow); } $i++; endwhile; endif;

因此,我使用 
$i

作为计数器来确定我想要父中继器中的哪一行。如果它是数组中的第二项,则为

$i = 1
,因为数组从
0
开始。您还必须在此函数中向其提供您的帖子 ID,但希望它有意义。如果您的父中继器或子中继器有很多行,请小心,为了保存它,您必须用刚刚创建的新中继器数组覆盖整个父中继器数组:

$field_key = "field_xxxxxxxxxxxxx" //FIELD KEY OF PARENT REPEATER 'media' update_field($field_key, $media_item, $postID);

这将保存新数组以及您附加到子中继器的行。我希望这对某人有帮助,因为我找不到任何涵盖此方法的示例。我希望添加到 ACF 中的功能,例如 
add_row()

,它适用于嵌套中继器!

    


1
投票

我制作了一个函数,它接受参数并在特定帖子(由 id 获取)内插入一个具有自己字段的转发器,并在该转发器内插入另一个或多个(因为它是转发器)具有自己字段的子转发器。

所以如果我有这样的东西:

剧透(复读机)

------------ 名称剧透(输入)

------------ 剧透标签(输入)

------------ 系带(子中继器)

------------------------- Nombre Enlace(输入)

------------------------- 缠绕(输入)

通过这个功能我可以插入X个“扰流板”和x个“Enlaces”

这里的功能:

function insert_field_subfield($repeater_field, $repeater_subfield, $field_values, $subfield_values, $field_key, $postid){ if(get_field($field_key, $postid)){ $value = get_field($field_key, $postid); }else{ $value = array(); } $value[] = $field_values; if(update_field( $field_key, $value, $postid)){ $i = 0; if( have_rows($repeater_field, $postid) ){ $spoiler_item = get_field($repeater_field, $postid); $total_rows = count(get_field($repeater_field, $postid)) -1; while( have_rows($repeater_field, $postid) ) : the_row(); if($i == ($total_rows)){ if( !is_array($spoiler_item[$i][$repeater_subfield]) ) { $spoiler_item[$i][$repeater_subfield] = array(); } if (count($subfield_values) == count($subfield_values, COUNT_RECURSIVE)){ // subfield_values is not multidimensional array_push($spoiler_item[$i][$repeater_subfield], $subfield_values); }else{ // subfield_values is multidimensional foreach($subfield_values as $subfield_value){ array_push($spoiler_item[$i][$repeater_subfield], $subfield_value); } } } $i++; endwhile; if(update_field($field_key, $spoiler_item, $postid)){ return true; }else{ return false; } } }else{ return false; }}

致电:

$addField = array("nombre_spoiler" => "Otro nombre", "spoiler_tag" => "Spoiler tag");

对于子中继器内的一个值:

$addSubField = array( 'nombre_enlace' => 'Valor de un array no multidimensional', 'enlace' => 'Valor enlace');

对于多个值:

$addSubField[] = array( 'nombre_enlace' => 'multidimensional', 'enlace' => 'Valor enlace'); $addSubField[] = array( 'nombre_enlace' => '2 multidimensional', 'enlace' => 'Valor enlace 2');

现在电话:

insert_field_subfield('spoiler', 'enlaces', $addField, $addSubField, 'field_586155ce49bf8', 21267); insert_field_subfield('spoiler', 'enlaces', $addField, $addSubField, 'field_586155ce49bf8', 21267);

我希望这对某人有用!

附注我它并不完美,因为我不控制第二个中继器,但这是没有 ACF API 的东西。

附注field_key 是第一个中继器的字段键。


0
投票
add_row

和父级的字段名称作为前缀。例如,当我在字段

documents
中嵌套字段
builtdocuments
时,我可以使用
$sub_fields = array(
    'current_document'   => "encrypted_file"
);  
  
add_row( 'builtdocuments_document', $sub_fields); 

成功添加嵌套中继器


0
投票

要更新嵌套转发器,请使用原帖中提到的 update_sub_field() 。

update_sub_field( array('media', 1, 'notes', 2, 'note'), 'Here is a new note!', $postID );

要添加新行,请使用 add_sub_row()

add_sub_row( array('media', 1, 'notes'), $note_vals, $postID );

如果 'notes' 有 sub_fields,$note_vals 可以是一个字段数组;如果没有,则可以是一个值。

这花了我太长时间才发现,所以我希望它对你有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.