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:
{ "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:
<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:
<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
maxthe Add button goes, and WordPress won't save the page with fewer thanminor more thanmax. - Only the right blocks.
allowedBlocksdecides what can be added, and the card only shows in the inserter inside this parent. - Edited the way it's shown.
editLayoutsets how the cards look while they're edited:grid(as on the page),stacked,accordion,tabsand more.
Which one to use
| Repeater field | Child blocks | |
|---|---|---|
| Each item is | a few simple values | a block with its own fields and markup |
| Edited in | the sidebar (or typed on the page) | the canvas, where it appears |
| Stored as | an array on one attribute | child blocks inside the parent |
| Good for | stats, logos, links, opening hours | cards, 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.