Loop Logic adds two template tags you can write in a snippet’s HTML tab:
<If>— show a block only when an attribute matches a condition (with optional<ElseIf>/<Else>).<Loop>— repeat a block for each row of a Repeater attribute, or over taxonomy terms or a posts query.
It works only when the “Enable Loop Logic” tool is on (Tools → Site Tools). The attributes you reference are defined on the snippet’s Attributes tab. In the HTML editor, the Loop Logic button opens a reference dialog and inserts ready-made examples.
Loop Logic supports nesting to any depth — <Loop> inside <Loop>, <If> inside <If>, and <If> inside <Loop> all work.
<If> — conditional blocks#
Show the inner content only when the condition is true.
<If field="attr_name" value="expected_value">
<!-- shown when the condition is true -->
</If>
<If field="attr_name" value="expected_value" operator="!=">
<!-- shown when NOT equal -->
</If>
<If field="attr_name" value="" operator="not_empty">
{{$attr_name}}
</If>
- field — the attribute name to test.
- value — the value to compare against (may be empty for
empty/not_empty). - operator — optional; defaults to
=(equals).
<If test="…"> — check a value#
test checks a value that is already filled in — a QuickCode or attribute placeholder — instead of an attribute by name:
<If test="{{acf.server_count.id.{{$provider}}}}">
{{acf.server_count.id.{{$provider}}}} servers
<Else />
no data
</If>
<If test="{{acf.free_tier.id.{{$provider}}}}" value="1">Free tier</If>
Alone it means “not empty”; add value / operator to compare. 0 counts as a value. If the value can contain double quotes, write the attribute in single quotes.
Operators#
| Operator | Aliases | Comparison |
|---|---|---|
= |
==, equals |
Equals (default) |
!= |
not, not_equals |
Not equals |
> |
greater |
Greater than (numeric) |
>= |
greater_equals |
Greater or equal (numeric) |
< |
less |
Less than (numeric) |
<= |
less_equals |
Less or equal (numeric) |
contains |
Contains the text | |
not_contains |
Does not contain the text | |
starts_with |
Starts with the text | |
ends_with |
Ends with the text | |
empty |
Value is empty | |
not_empty |
Value is not empty | |
in |
In a comma-separated list (value="a,b,c") |
|
not_in |
Not in a comma-separated list | |
true |
Value is truthy (1/true/yes/on) |
|
false |
Value is falsy |
<ElseIf> / <Else> branches#
An <If> block can contain <ElseIf …> branches and a final <Else> (these are separators — no closing tags). The first matching branch is shown; if none match, <Else> is used. Write the markers self-closing:
<If field="plan" value="pro">
Pro features
<ElseIf field="plan" value="trial" />
Trial features
<Else />
Upgrade now
</If>
The plain form (<ElseIf …>, <Else>) is processed exactly the same, but the code editor reads it as an unclosed tag: indentation of the rest of the block drifts, tag matching and folding break, and auto-close can insert a stray </Else>. The Loop Logic dialog has a Convert branch markers button that rewrites the plain form to self-closing in one step (a single undo reverts it).
<Loop> — repeater loops#
Repeat the inner template once per row of a Repeater attribute. Field names must match the sub-fields defined for that repeater in the Attributes tab.
<Loop field="my_repeater">
<div>{{loop.title}}</div>
<div>{{loop.description}}</div>
</Loop>
Placeholders inside a loop#
| Placeholder | Description |
|---|---|
{{loop.field_name}} |
Value of a sub-field in the current row |
{{field_name}} |
Short alias — same as above |
{{loop.index}} |
Current row number, starting at 1 |
{{loop.index0}} |
Current row number, starting at 0 |
{{loop.is_first}} |
true on the first row |
{{loop.is_last}} |
true on the last row |
<If> inside a <Loop>#
Conditions inside a loop are checked per row, against that row’s fields and the loop meta:
<Loop field="items">
<div class="item">
<h3>{{loop.title}}</h3>
<If field="status" value="active">
<span class="badge">Active</span>
</If>
<If field="loop.is_first" value="true"><hr></If>
</div>
</Loop>
Nested loops#
A <Loop> can contain another <Loop>:
<Loop field="groups">
<h3>{{name}}</h3>
<ul>
<Loop field="items">
<li>{{loop.title}}</li>
</Loop>
</ul>
</Loop>
<Loop> over terms / posts#
A loop can also pull live WordPress data with a source attribute. The same {{loop.field}} placeholders and per-row <If> apply.
source="terms" — taxonomy terms#
<Loop source="terms" taxonomy="category" hide_empty="true" orderby="name" order="ASC" number="10">
<a href="{{loop.link}}">{{loop.name}} ({{loop.count}})</a>
</Loop>
Attributes: taxonomy (default category), hide_empty (default true), number, orderby, order, parent. Row fields: id, name, slug, link, description, count.
source="posts" — posts query#
<Loop source="posts" post_type="post" posts_per_page="5" orderby="date" order="DESC">
<a href="{{loop.link}}">{{loop.title}}</a>
<div>{{loop.thumbnail_html}}</div>
</Loop>
Attributes: post_type (default post), posts_per_page (default 5), orderby, order, category (term ID), or taxonomy + term (term slugs, comma-separated). Row fields: id, title, link, excerpt, date, thumbnail (URL), thumbnail_html (<img> tag).
⚠ These run a live query on every page render and are checked per request, so behind full-page caching the cached page is served without re-querying.
Notes#
- ⚠ Nothing is processed unless Enable Loop Logic is on.
- An unbalanced tag (a missing
</If>or</Loop>) is shown as plain text rather than breaking the rest of your markup. - A loop over a missing/empty repeater (or an empty query) outputs nothing.
Example: a pricing card from block fields#
Attributes: plan (Text), price (Number), featured (Toggle), features (Repeater text:text,included:toggle).
<div class="plan<If field="featured" value="1"> plan--featured</If>">
<If field="featured" value="1"><span class="plan__badge">Most popular</span></If>
<h3>{{$plan}}</h3>
<p class="plan__price">${{$price}}</p>
<ul>
<Loop field="features">
<li class="<If field="included" value="1">yes<Else />no</If>">{{loop.text}}</li>
</Loop>
</ul>
</div>
Step by step: Build a pricing table block.