我正在运行ACF 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( array('media', 1, 'notes', 2, 'note'), 'Here is a new note!', $postID );
无论是否已经有一行,我如何将行添加到嵌套的中继器中?
发布于 2016-07-15 18:54:51
我能够通过ACF的函数和常规的好的旧PHP的结合来实现这一点。我找不到另一种方法,但我读到了其他人提到这种方法的帖子,尽管他们没有详细说明或显示代码,所以下面是我完成这个方法的冗长步骤:
array_push()
追加子中继器行update_field()
再次保存整个父数组为了帮助说明父/子关系,请记住我有一个名为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个元素,一个在嵌套中继器中没有元素。这一点很重要,因为您需要确定在嵌套的中继器中是否已经建立了一个数组,然后才能添加到它中。
//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
开始。您还必须在此函数中将post ID提供给它,但希望这是有意义的。如果您为父或子中继器有许多行,请小心,为了保存它,您必须用刚刚创建的新中继器数组覆盖整个父中继器数组:
$field_key = "field_xxxxxxxxxxxxx" //FIELD KEY OF PARENT REPEATER 'media'
update_field($field_key, $media_item, $postID);
这将使用附加到子中继器的行保存新数组。我希望这能帮助到一些人,因为我找不到任何关于这个方法的例子。我很想加入到ACF中,比如add_row()
,它适用于嵌套的中继器!
发布于 2016-12-27 18:27:37
这就是我为我做的,我希望这对你们有用。
我做了一个函数,接受参数并在特定的帖子(由id接收)中插入一个中继器和这个中继器,在这个中继器内插入另一个或多个(因为它是一个中继器)和他自己的字段。
所以如果我有这样的东西
扰流器(复读机)
有了这个功能,我可以插入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);
我希望这会对某些人有用!
这并不完美,因为我没有控制第二个中继器,但这是没有API的东西。
P.S. field_key是第一个中继器的字段键。
发布于 2021-08-30 04:12:41
当我有一个嵌套的中继器时,最终为我工作的是使用带有父字段名的add_row
作为前缀。例如,当我在字段documents
中嵌套字段builtdocuments
时,我可以使用
$sub_fields = array(
'current_document' => "encrypted_file"
);
add_row( 'builtdocuments_document', $sub_fields);
成功地添加到嵌套中继器
https://stackoverflow.com/questions/38384500
复制相似问题