$scores = $page->children()->nth(1)->children()
<?php foreach ($scores as $score) : ?>
//looping works fine here and now is printing als scores since I added an index to the result folders
<?php endforeach ?>
My main question is what is the best way to read the content of different subfolders from a page.
$scores = new Pages();
if ($rankings = $page->children()->find('rankings')) {
$scores = $rankings->children()->listed();
}
if ($scores->isNotEmpty()) {
foreach ($scores as $score) {
// do stuff
}
}
Why? Because we can’t be sure the page we are trying to get exists. And if rankings doesn’t exist, calling children() will throw an error. That’s why we first check if the page exists before then fetching the children. Also, using nth() might give you the right page now, but what if the order of the children is changed? So fetching by page id seems better (even though the id could also change). An alternative would be to find by template.
In the next step, I check if the collection is empty or not, because I don’t want to render emtpy HTML tags.