WP ALL按xml节点属性导入加载图像过滤

时间:2018-02-21 20:32:38

标签: xml wordpress simplexml wpallimport

我有这个xml节点

salesPhases () {
  let phases = this.$store.state.addresses.salesPhases
  let uniquePhases = []
  for (let i = 0; i < phases.length; i++) {
    if (!uniquePhases.find(x => x.phase_number === phases[i].phase_number)) {
      uniquePhases.push(phases[i])
    }
  }
  return uniquePhases.sort((a, b) => {
    return a.phase_number - b.phase_number
  })
}

我想用属性“planimetria = 1”拆分图像,并将file_path写入自定义字段。

我不能使用wpallimport的[FOREACH]方法我尝试调用名为set_planimetrie的函数(file_allegati [1])

我写了这个php函数,但它不起作用。

<file_allegati>
  <allegato id="0" planimetria="0" type="foto">
    <id>0</id>
    <file_path>https://##.jpg </file_path>
  </allegato>
  <allegato id="1" planimetria="1" type="planimetria">
    <id>1</id>
    <file_path>https://##.jpg </file_path>
  </allegato>
</file_allegati>

1 个答案:

答案 0 :(得分:0)

您只获取foreach的第一个值,因为您在foreach中使用了return语句。

$allegato['type']的返回类型为SimpleXMLElement。您可以使用__toString获取字符串或使用(string)

如果您将return语句放在foreach之外,并使用(string)获取您的值,您的代码将如下所示:

function set_planimetrie($allegati)
{
    $result = '';
    $xml = new SimpleXMLElement($allegati);

    foreach ($xml->children() as $allegato) {
        if ((string)$allegato['type'] === 'planimetria' && (string)$allegato['planimetria'] === "1") {
            if ($result !== '') {
                $result .= ',';
            }
            $result .= $allegato->file_path;
        }
    }
    return $result;
}

Output php example

相关问题