我使用字段函数将循环中的内容添加到子中继器字段中。
我已经到了数据被添加到子中继器的地步,但它正在覆盖自己。它只更新子中继器中的第一行,所以我只看到最后一个循环。
我使用$counter
迭代父中继器,但是当我试图向子中继器添加迭代时,它会中断函数。就像这样:
update_sub_field( array($field_key, $counter, $sub_field_key, $rowcount), $value, $post_id );
我试过另一个函数add_sub_row
..。这会将正确的行数添加到子中继器中,但不会添加数据。
这是我的完整代码:
// Product Code Titles
$rows = $html->find('div[class=product-table]', 0)->find('tr');
$field_key = "field_5ae0882f9d6f9";
$sub_field_key = "field_5ae088999d6fb";
if(empty($rows)){
die("Empty array");
}
$titles = array(); // aka your $data
// here we fetch the first row and iterate to get titles
$row = current($rows);
foreach ($row->find('td') as $cell) {
$titles[] = array("column_title" => strip_tags($cell->innertext));
}
update_field( $field_key, $titles, $post_id );
// here we continue iteration starting from second row
$value = array();
$rowcount = 1;
while($row = next($rows)){
$cells = $row->find('td');
$columnsCount = count($cells);
$counter = 1;
foreach ($cells as $cell) {
$value[] = array("text" => strip_tags($cell->innertext));
update_sub_field( array($field_key, $counter, $sub_field_key), $value, $post_id );
echo '<pre>'; print_r($value); echo '</pre>';
$value = array();
$counter++;
}
$rowcount++;
}
为了给出一些上下文,我正在重新创建这个表,将单元格数据放入子中继器字段中。
发布于 2018-06-28 12:21:09
在这个问题上没有太多的内容,所以希望这能帮助到别人。
我停止了它对$value
数组的附加,将函数更改为update_sub_row
,并添加了一个计数器来迭代子中继器行。
下面是完整的代码:
// Product Code Titles
$rows = $html->find('div[class=product-table]', 0)->find('tr');
$field_key = "field_5ae0882f9d6f9";
$sub_field_key = "field_5ae088999d6fb";
if(empty($rows)){
die("Empty array");
}
$titles = array(); // aka your $data
// here we fetch the first row and iterate to get titles
$row = current($rows);
foreach ($row->find('td') as $cell) {
$titles[] = array("column_title" => strip_tags($cell->innertext));
}
update_field( $field_key, $titles, $post_id );
// here we continue iteration starting from second row
$value = array();
$rowcount = 1;
while($row = next($rows)){
$cells = $row->find('td');
$columnsCount = count($cells);
$counter = 1;
foreach ($cells as $cell) {
$value = array("field_5ae088b79d6fc" => strip_tags($cell->innertext));
update_sub_row( array($field_key, $counter, $sub_field_key), $rowcount, $value, $post_id );
echo '<pre>'; print_r($value); echo '</pre>';
$value = array();
$counter++;
}
$rowcount++;
}
https://stackoverflow.com/questions/51078678
复制相似问题