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条)

报告相同问题?

悬赏问题

  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?