GCBGutenberg
Control Blocks
v1.3.3
Buy

Repeater fields in a Gutenberg block

Two ways to repeat things in a custom block: rows of fields in the sidebar, or whole blocks on the canvas with limits.

Most custom blocks repeat something: stats, logos, cards, slides, FAQ items. Core Gutenberg has no repeater field, so you either build one in React or reach for a plugin. In Gutenberg Control Blocks (GCB) there are two kinds of repeater, and the right one depends on what each item is.

  • A repeater field for simple items with a few values each, like stats (a figure and a caption), logos, or a row of links. The rows live in the sidebar.
  • Child blocks for rich items that are blocks in their own right, like cards with a picture, words and a button, or accordion items. They live on the canvas, and your client adds them where they appear.

A repeater field: rows in the sidebar

Add a field of type repeater to block.fields.json, and list the fields one row has:

block.fields.json
{  "controls": [    { "id": "stats", "attributeKey": "stats", "type": "repeater", "label": "Stats",      "addButtonLabel": "Add stat", "collapsedTitle": "value", "max": 4,      "fields": [        { "id": "value", "attributeKey": "value", "type": "text", "label": "Figure" },        { "id": "label", "attributeKey": "label", "type": "text", "label": "Caption" }      ] }  ]}

The sidebar shows the rows, with Add, remove and drag to reorder. min and max limit the rows, and collapsedTitle picks which value names a row when it's folded. A row can hold any field type, including images, links and icons.

In the template, loop over the rows:

render.php
<ul <?php echo get_block_wrapper_attributes(['class' => 'stats']); ?>>  <?php foreach ((array) ($attributes['stats'] ?? []) as $i => $row) : $row = (array) $row; ?>    <li>      <b data-gcb-row="<?php echo $i; ?>" data-gcb-subfield="value"><?php echo esc_html($row['value'] ?? ''); ?></b>      <span data-gcb-row="<?php echo $i; ?>" data-gcb-subfield="label"><?php echo esc_html($row['label'] ?? ''); ?></span>    </li>  <?php endforeach; ?></ul>

The data-gcb-row and data-gcb-subfield attributes are optional. With them, your client can also type each figure and caption right on the page.

Child blocks: repeat on the canvas

When each item is rich enough to be a block of its own, make it one. The card is an ordinary block folder with its own fields. The parent's template marks where the cards go with a <Repeater>, which also holds every rule about them:

render.php
<section <?php echo get_block_wrapper_attributes(['class' => 'cards']); ?>>  <Repeater    allowedBlocks='["gcb/card"]'    addButtonLabel="Add card"    min="1"    max="6"    defaultChildren="3"    editLayout="grid"  /></section>
  • Where it sits. Your client adds, moves and removes cards on the canvas, in the design, not in a list in the sidebar.
  • Limits that hold. At max the Add button goes, and WordPress won't save the page with fewer than min or more than max.
  • Only the right blocks. allowedBlocks decides what can be added, and the card only shows in the inserter inside this parent.
  • Edited the way it's shown. editLayout sets how the cards look while they're edited: grid (as on the page), stacked, accordion, tabs and more.

Which one to use

Repeater fieldChild blocks
Each item isa few simple valuesa block with its own fields and markup
Edited inthe sidebar (or typed on the page)the canvas, where it appears
Stored asan array on one attributechild blocks inside the parent
Good forstats, logos, links, opening hourscards, slides, tabs, FAQ items, team members

More in the docs: the repeater field and InnerBlocks, the repeater and areas. Or try a repeater in your browser.

Build anything in the block editor

Every licence includes all features and a year of updates and support.

Buy GCB from $8.25 a month See pricing