Processing and Grouping Multiple Elements with FOREACH Loops

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.

1

My Caption
Another Caption
We will use these for alt text
More text

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

[FOREACH({images/img})]
{.}
[ENDFOREACH]

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.

 
 

Related

Inline PHP

IF Statements

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注