2016-07-14 86 views
1

我正在運行ACF PRO 5.3.9.2,但我認爲這適用於帶嵌套中繼器的任何版本。我希望使用update_field()update_sub_field()函數將行添加到嵌套中繼器(即中繼器內的中繼器)。在Wordpress高級自定義字段中添加行到嵌套中繼器PRO

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

但這沒什麼。沒有辦法確定WHICH「媒體」中繼器我希望添加一個「註釋」。

我可以使用update_sub_field()像下面的IF代碼一樣成功地「更新」嵌套的中繼器字段,並且只有在嵌套的中繼器字段中存在一行時,它纔會覆蓋一行而無法添加。如果轉發器中沒有一行,它也不會工作。

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

我能做些什麼來添加行到嵌套的中繼器是否有一行已經或沒有?

回答

1

我能夠通過ACF的功能和常規的舊版PHP的結合來實現這一點。我無法找到另一種方法,但我讀其他職位,人們暗示這種方法,雖然他們不詳細或不顯示代碼,所以這裏是我的囉嗦的步驟來實現:

  1. 獲得整個你的父母中繼
  2. 環路的陣列通過,直到你打你要編輯
  3. 圖,如果它是一個數組或沒有嵌套轉發行(如果不是,讓一個數組)
  4. 使用array_push()附加兒童中繼器行
  5. 保存整個父數組ag艾因使用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種元素在父中繼器,其中一個具有在嵌套中繼器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提取)中插入一個帶有自己字段的中繼器,並且在此中繼器中插入另一個或多個帶有自己的字段的一個或多個(因爲它是中繼器)subrepeater。

所以,如果我有這樣的事情:

擾流器(Repeater)

------------農佈雷擾流板(輸入)

----- -------擾流板標籤(輸入)

------------ ENLACES(副中繼器)

------------ ------------- Nombre Enlace(輸入)

------------------------- Enlace(輸入)

使用此函數我可以插入X「擾流板」和x「 ENLACES「S

這裏的功能:

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

對於內subrepeater一個值:

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

我希望這將是爲別人有用!

P.S.我不完美,因爲我不控制第二個轉發器,但這是沒有ACF API的東西。

P.S. field_key是第一個中繼器的字段密鑰。

相關問題