cafe_rows()
Purpose
Returns the items of a Collection or the sections of a Section Builder as row objects, ready for foreach. A Field Set is returned as one row. No global loop state: rows nest naturally and can be passed to template parts.
Syntax
cafe_rows( string $selector, mixed $object_id = null, ?string $object_type = null ): array
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
$selector |
string | - | Field name or key. |
$object_id |
mixed | null |
Object reference like in cafe_get_field() (null = current post). |
$object_type |
string|null | null |
Object type, or null to infer it. |
Return value
A list of Cybo\AdvancedFieldEngine\API\FieldRow objects; an empty array when there are no rows (never null).
| Method | Returns |
|---|---|
get( $name, $default = null ) |
A sub-field value (formatted like cafe_get_field()), or $default |
has( $name ) |
Whether the sub-field has a value |
rows( $name ) |
Rows of a nested Collection / Section Builder / Field Set |
section() |
Section name of a Section Builder row, e.g. "hero"; null otherwise |
number() |
Position, starting at 1 |
is_first() / is_last() |
Whether this is the first / last row |
to_array() |
All sub-field values keyed by name |
Rows also support array access ($row['title']), foreach over their values, count() and wp_json_encode().
Example
<?php foreach ( cafe_rows( 'slides' ) as $slide ) : ?>
<div class="slide<?php echo $slide->is_first() ? ' is-active' : ''; ?>">
<h2><?php echo esc_html( $slide->get( 'title' ) ); ?></h2>
<?php foreach ( $slide->rows( 'buttons' ) as $button ) : ?>
<a href="<?php echo esc_url( $button->get( 'url' ) ); ?>"><?php echo esc_html( $button->get( 'label' ) ); ?></a>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
<?php
// Section Builder: one template part per section.
foreach ( cafe_rows( 'page_sections' ) as $section ) {
get_template_part( 'sections/' . $section->section(), null, [ 'section' => $section ] );
}
Notes
- Rows are read-only; save changes with
cafe_update_field(). - All rows and sub-field values of a post load in bulk (a constant number of queries), so looping is cheap.
cafe_get_field()returns the same data as plain arrays (nullwhen there are no rows).