【问题标题】:Update grandchild repeater field with value per row用每行的值更新孙中继器字段
【发布时间】:2018-12-19 16:41:31
【问题描述】:

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

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

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

我哪里出错了(见底部的代码)?所以对于第一个表,每个信息表中每列应该有4行数据。

  • 顶部中继器(产品代码):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++;

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案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 table 中的 first information 行:

  • 使用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++;
    

    【讨论】:

    • 好的,让我根据你的循环写一个例子。
    • 当然。我在当前答案中使用的那些只是示例,因此您可以了解如何生成祖先,如update_sub_row( $ancestors, $i, $row, $post_id );
    • 请查看更新后的答案。 (或示例循环)
    • 再次检查代码,我在之前的评论之后做了一些修改。
    猜你喜欢
    • 2018-02-17
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多