使用每行的值更新孙中继器字段

时间:2018-07-11 15:08:24

标签: php wordpress advanced-custom-fields

我正在尝试从表中获取以下数据:

enter image description here

并将其输入到嵌套的ACF repeater field中。我非常接近它正在创建正确数量的表(该示例中为3),为每个表创建正确的列数。

最后一部分不是很有效,只是将最后一行数据输入到“信息”中继器中,这表明它没有迭代行号,因此只将其输入到第1行。

我在哪里出错(请参见底部的代码)?因此,对于第一个表,每个信息表的每一列都应包含4行数据。

enter image description here

  • 顶级中继器(产品代码):field_5ae0882f9d6f9
    • 嵌套中继器1(表):field_5b3f409de191a
      • 嵌套中继器2(信息):field_5ae088999d6fb
        • 要更新的字段(文本):field_5ae088b79d6fc

代码如下:

     $value = array();
      $rowcount = 1;
      while($row = next($rows)){

          $cells = $row->find('td');
          $columnsCount = count($cells);
          $counter = 1;
          foreach ($cells as $cell) {
            $value = array(
              "field_5ae088999d6fb" => array(
                array("field_5ae088b79d6fc" => strip_tags($cell->innertext))
              )
            );
            update_sub_row( array('field_5ae0882f9d6f9', $tablecounter, 'field_5b3f409de191a'), $counter, $value, $post_id );
            $value = array();
            $counter++;
          }

        $rowcount++;

  }

  $tablecounter++;

1 个答案:

答案 0 :(得分:1)

请参考以下 my 字段结构:

Field Name        Field Key           Type
product_codes     field_5b4d8f7246886 Repeater
  - table         field_5b4d8fd946888 Repeater
    - information field_5b4d906246889 Repeater
      - text      field_5b4d907f4688a Text

以下是更新 first information中的 first table行的方法:

  • 使用update_sub_row()

    // Here, we build the ancestors, starting from the TOPMOST LEVEL down to the
    // field that we're updating its value.
    $selector = [
        'field_5b4d8f7246886', 1, // selects the first `table` in `product_codes`
        'field_5b4d8fd946888',    // selects ALL the `information` rows
    ];
    
    update_sub_row( $selector, 1, [
        'field_5b4d906246889' => [
            [ 'field_5b4d907f4688a' => '040-811' ],
            [ 'field_5b4d907f4688a' => '040-821' ],
            [ 'field_5b4d907f4688a' => 'Standard Size Fibre Optic Handle' ],
            [ 'field_5b4d907f4688a' => '1' ],
        ],
    ] );
    

  • 使用update_sub_field()

    // Here, we build the ancestors, starting from the TOPMOST LEVEL down to the
    // field that we're updating its value.
    $selector = [
        'field_5b4d8f7246886', 1, // selects the first `table` in `product_codes`
        'field_5b4d8fd946888', 1, // selects the first `information` row
        'field_5b4d906246889',    // finally, selects all `text` columns
    ];
    
    update_sub_field( $selector, [
        [ 'field_5b4d907f4688a' => '040-811' ],
        [ 'field_5b4d907f4688a' => '040-821' ],
        [ 'field_5b4d907f4688a' => 'Standard Size Fibre Optic Handle' ],
        [ 'field_5b4d907f4688a' => '1' ],
    ] );
    

  • 希望对您有所帮助,如果您需要进一步的帮助,请告诉我。


    更新

    尝试以下代码:

    $the_row = [
        'field_5ae0882f9d6f9', $tablecounter,
        'field_5b3f409de191a',
    ];
    
    //$value = array();
    $rowcount = 1;
    foreach ( $rows as $row ) {
    
        $cells = $row->find('td');
        //$columnsCount = count($cells);
        //$counter = 1;
        $cols = [];
        foreach ($cells as $cell) {
            $value = strip_tags($cell->innertext);
            $cols[] = [ 'field_5ae088b79d6fc' => $value ];
            //$counter++;
        }
    
        update_sub_row( $the_row, $rowcount, [
            'field_5ae088999d6fb' => $cols,
        ], $post_id );
    
        $rowcount++;
    
    }
    
    $tablecounter++;