Processing and Grouping Multiple Elements with FOREACH Loops

WP All Import supports a FOREACH syntax that makes it easy to make adjustments to multiple elements from your file at the same time. This is useful when building custom HTML in the Content section, or when preparing other data from your file for fields that require a specific format.

Here's an example showing how to implement a FOREACH loop in WP All Import.

Here's an example XML file that contains some image information – It is assumed the default (/element) was chosen as the XPath in Step 2.

    <img url='1-1.jpg'>My Caption</img>
    <img url='1-2.jpg'>Another Caption</img>
    <img url='1-3.jpg'>We will use these for alt text</img>
    <img url='1-4.jpg'>More text</img>

Here’s an example import template where FOREACH is used to loop through each img element in the above XML.

<img src="{@url}" alt="{.}" />

Passing {images/img} to the FOREACH means we will loop through each element matching that XPath. To get this XPath, just drag one of the matching elements from the XML tree on the right to the post content box. You’ll get something like {images/img[1]}. Just remove the [1] and you’re good to go.

Inside the FOREACH loop, the XPath is relative. You can’t access “higher-level” elements inside the FOREACH loop. For example, you wouldn't be able to access the 'id' element from within the FOREACH loop.

{@url} will return the url attribute of the element you are looping through - '1-1.jpg', '1-2.jpg', etc.

{.} will return the value of the element you are looping through – in this case, each img - 'My Caption', 'Another Caption', etc.

{element_name} will return the value of the element_name child element of the element you are looping through.




Inline PHP

IF Statements