dongren4147 2016-07-14 21:23
浏览 75
已采纳

在Wordpress高级自定义字段PRO中向嵌套转发器添加行

I am running ACF PRO 5.3.9.2, but I think this pertains to any version with nested repeaters. I am hoping to add rows to a nested repeater (that is a repeater inside a repeater) using the update_field() or update_sub_field() function.

The update_field() function works great for first-level repeater rows, i.e.:

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

but I am uncertain about how to use this for nested repeaters. This is the ACF Fields structure:

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

So, here is the code I am attempting:

$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 );

But this does nothing. There is no way to determine WHICH "Media" repeater I am wishing to add a "note" to.

I can successfully "update" the nested repeater field using update_sub_field() like the code below IF and only IF there is a row in the nested repeater field, but it overwrites just the one row and cannot add to it. It also will not work if there isn't currently a row in the repeater.

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

What can I do to add rows to a nested repeater whether there is a row already or not?

  • 写回答

2条回答 默认 最新

  • dongmi5607 2016-07-15 18:54
    关注

    I was able to accomplish this with both a combination of ACF's functions and regular good old PHP. I can't find another method, but I read other posts where people allude to this method, although they do not elaborate or show code, so here were my long-winded steps to accomplish this:

    1. Obtain the entire array of your parent repeater
    2. Loop through until you hit the row with the nested repeater you want to edit
    3. Figure out if it is an array or not (if it isn't, make it an array)
    4. Append the child repeater row using array_push()
    5. Save the entire parent array again using update_field()

    To help illustrate the parent/child relation, remember I have a repeater field called media and a nested repeater within that called notes, which only has the 1 sub-field note. So if I use get_field('media') this is the array it would return:

    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] => 
        )
     )
    

    So, in this example, I have 2 elements in the parent repeater, one of which has 2 elements in the nested repeater, one that has no elements in the nested repeater. This is important because you will need to determine if there is already an array established in the nested repeater before you can add to it.

    //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;
    

    So, I use $i as a counter to determine which row in the parent repeater I want. If it was the second item in the array, $i = 1, since the array starts at 0. You also have to feed it your post ID in this function, but hopefully it makes sense. Be careful if you have many rows for your parent or child repeaters, as to save it, you have to overwrite the entire parent repeater array with the new one you just created:

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

    This will save the new array with the rows you appended to your child repeater. I hope this helps someone as I couldn't find any examples covering this method. I would love to a function added to ACF like add_row() which works for nested repeaters!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?