Jump to content

all repeater fields access in a loop fail


danielsl
 Share

Recommended Posts

i have a page with similar repeater fields of various names, and similar output in a loop

so i really don't really want to write endless foreach repeater for rendering

but need to aggregate all repeater fields and then pass them via one foreach loop


 $repeaters=$pages->get("/advert_page/");


    foreach($repeaters->fields as $repeator){
		
	if($repeator=="title")continue;


		echo $repeator->summary;
		echo $repeator->images->first;

}

it simply doesnt work,

so i have to manually do for each repeater

$repeater=$pages->get("/advert_page/");
foreach($repeater->Media as $m) {
    echo "<img src='{$m->images->first()->size(120, 60)->url}'>";   
     echo "<p>Year built: {$m->summary} </p>";
     }
    echo '</div><div class="uk-grid uk-grid-width-medium-1-4" data-uk-grid-margin="">"';
foreach($repeater->featured as $m) {
    echo "<img src='{$m->images->first()->size(120, 60)->url}'>";   
     echo "<p>Year built: {$m->summary} </p>";

 

Link to comment
Share on other sites

Welcome to the forums, @danielsl! :)

You could do this:

$p = $pages->get('/advert_page/');
$repeater_names = $p->fields->find('type=FieldtypeRepeater')->explode('name');
foreach($repeater_names as $repeater_name) {
    foreach($p->$repeater_name as $item) {
        // output your repeater item
    }
}

If you have repetitive markup that is used in several places also remember that you can put it an in an include to avoid repeating yourself.

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...