Righto, so I was half way through putting together an example when I realised it’d actually be a super simple change to tie this into the existing repeater functionality. It’s not documented yet, but if you download the latest version you can change the repeater items of a component with a filter. To do so, add a component with repeatable content ( e.g. a tabs component ) and take note of the Node ID:
Then you can add the following to your child theme’s functions.php
. Note that I’ve used some shoddy function naming here, I’d recommend a more solid approach in your production code:
function custom_repeater_items($items, $context, $tag) {
// ensure that we're dealing with the correct node
if ( array_key_exists('instanceId', $tag) && $tag['instanceId'] !== 'h_v78-oQaHFVTxsPI5-YO' ) return $items;
// return an array of repeater items.
return [
[
'repeaterId' => 'tab-1',
'componentDetails' =>
[
'tabButton' =>
[
'content' =>
[
'type' => 'value',
'value' => 'Tab 1',
]
],
'tabContent' =>
[
'content' =>
[
'type' => 'value',
'value' => 'Content 1'
]
]
]
],
[
'repeaterId' => 'tab-2',
'componentDetails' =>
[
'tabButton' =>
[
'content' =>
[
'type' => 'value',
'value' => 'Tab 2',
]
],
'tabContent' =>
[
'content' =>
[
'type' => 'value',
'value' => 'Content 2'
]
]
]
]
];
}
add_filter( 'gust_repeater_items', 'custom_repeater_items', 10, 3 );
This is getting into some advanced use cases for Gust, but is exactly what it was created to handle.
Apart from this, the best way would be to set the context on a parent element as filter
, set your filter name and return your data in php. E.g:
function my_custom_data() {
return [
[ 'title' => 'title 1', 'content' => 'content 1' ],
[ 'title' => 'title 2', 'content' => 'content 2' ],
]
}
add_filter( 'my_custom_data', 'my_custom_data' );
You can then add a child element and set the context option to “Use Context Loop” which will loop over the above data. Within any children inside that loop, you can set the content type to “filter” and return the data you need:
function custom_data_item_title( $value, $item ) {
return $item['title'];
}
add_filter( 'custom_data_item_title', 'custom_data_item_title' );
That’s very similar to what we do in the blog post tutorial, except we’re using filters instead of functions for the content.
Hopefully that makes sense!