EACH Loop Tag

Provides easy looping through related entries (from relationship fields), taxonomies and media upload fields within Pods Templates.

Overview

The [each]  tag is used for looping through relationship fields, taxonomy and media upload fields within your Pods Templates. When you’re inside the loop of the field or taxonomy or image, you can use magic tags for the fields within each of those objects.

Quick Start Guide

  • Start your [each] loop with the relationship field, taxonomy or image field: [each relationship_field], [each taxonomy]  or [each image_field]
  • Inside the loop, use the magic tag of the fields inside the relationship, taxonomy or image field (ie post_title, permalink).
    NOTE: Do not repeat the relationship field, taxonomy or image field inside the loop or you’ll step out of the loop (ie Don’t use {@relationship_field.post_title}, it’s just {@post_title}.
  • End your loop with another [/each]  tag.

EACH Block for Simple Repeatable Fields

This example shows how to loop through all values in a Simple Repeatable Field added in Pods 2.9:

[if buy_now_links]
    <ul class="buy-now-link-list">
    [each buy_now_links]
        <li><a href="{@_value,esc_url}">Buy Now on {@_value,pods_host_from_url}</a></li>
    [/each]
    </ul>
[else]
    <p>This book is not available for sale.</p>
[/if]

This code would output one list item (<li>)  with the buy now link value for each value input into the Simple Repeatable Field.

The two custom magic tags available to you in this case are {@_value} to output each value and {@_index} to output the position (starting at 0) of the value. Using {@_index} may most be useful in [if] conditionals.

EACH Block for Relationship Field

This example shows how to loop through all entries in a relationship field—in this case a multi-select relationship field called chapters in a custom post type book—adding markup to each entry:

<h3>Chapters</h3>
<ul class="chapters-list">
[each chapters]
    <li><a href="{@permalink,esc_url}">{@post_title}</a></li>
[/each]
</ul>

This code would output one list item (<li>)  with the post_title for each related chapter post.

NOTE: When you’re inside the [each field_name]  block, you do not need to prefix your relationship based magic tags with the {@field_name}, like {@field_name.post_title}, just use {@post_title}. When you’re in a multiple select loop, the each is actually creating the loop, so prefixing the post_title  or permalink  with the field name in front of it inside the each  loop will break you out of the loop. Many people run into this mistake when making each blocks.

You cannot use the Each Block to loop through a Simple Relationship Field (Predefined or Custom).

Each Block for Taxonomy

[each taxonomy-name]
    <li><a href="{@permalink,esc_url}">{@name}</a></li>
[/each]

Each Block for Images

If you’re created a multiple image field, you can also each through that connection to output a grid or gallery. Of course, if you activated the “Output as WordPress Gallery” you only have to type {@gallery}.

[each gallery_images]
    <div>{@_img.medium}</div>
[/each]

Note that this uses the Special image handling Magic Tags. You could reasonably also do the following:

[each gallery_images]
    <div><img src="{@_src.medium,esc_url}" class="gallery" /></div>
[/each]

Limitations of the EACH Block

You cannot use the each loop to order in a specific way, or filter specific records or only show a specific number of records. each is designed to loop through everything on the other side of the loop, no ordering, no limiting, no filtering. If you need more control, you need to be using PHP Theme Templates.

Other Helpful Documentation on Template Tags

BEFORE and AFTER

Provides the ability to run a opening block in your template that will be output BEFORE and AFTER the loop.

Example Template Tag Usage

Examples of Pods Templates using a mixture of tags

IF/ELSE Conditional Tag

Provides simple conditional logic for your Pods Templates

ONCE Template Tag

DO NOT USE. This Tag has been deprecated. Use CSS with nth positioning :first or the if shortcode if you need this functionality.