Hi
I use the repeater field to create a dynamically structured page which different “strips” for example:
the_content() – default wordpress
normal section – a WYSIWYG field
image
normal section
This allows the writers to build out a page in a more interesting way and allows us to have full width images etc.
What I would like to do though is say every 3rd or random repeater entry to add in a advert. This is something I would want to do automatically rather than having another repeater option to manually insert.
Is there a way to effect the “loop” to insert another bit of code?
p.s after using ACF for a couple years, still can not get over how amazing this is!
Hi @kyon147
Sure there is. Let’s assume that you’re using have_rows for looping out your repeater. Here’s an example of how you could do it:
<?php
if( have_rows('repeater') ){
$i = 1;
while( have_rows('repeater') ){
the_row();
//stuff
the_sub_field('subfield');
//advert after every third row
if( $i % 3 == 0 ){
//advert code
}
$i++;
}
}
?>