Getting all children from a page with the title 'projects'

In my site there are two toplevels. Under those levels in both cases there is a page named ‘projects’.
I want to get all the children from both pages as:

for each project page-> all the children.

This is my code but I get an error. What am I doing wrong?
It seems like I cant filter through page title?

<?php foreach($site->children()->children()->find('projects')->children() as $thumb): ?> <?php snippet('thumb', ['thumb' => $thumb]); ?> <?php endforeach ?>

Please wrap code blocks within three backticks on a separate line before and after the code block. Otherwise it is not readable. Thank you.

And if you get an error, please tell us what sort of error.

<?php foreach($site->children()->children()->find('projects')->children() as $thumb): ?> <?php snippet('thumb', ['thumb' => $thumb]); ?> <?php endforeach ?>

Error

Call to a member function children() on null

You have to check if this page exists. Also, find() needs the id of a page, you only pass the slug. I’d filter by template instead.

I get it working with id’s and one single if argument. If it doesnt exists I get an error.

<?php if ($site->children()->find('2019/projects')->exists()): ?>
<?php $thumbs = $site->children()->find('2019/projects', '2020/projects')->children()->listed() ?> 

<div class="is-container-column is-center blogresult">  
<?php foreach($thumbs as $thumb): ?>
<?php snippet('subthumb', ['thumb' => $thumb]); ?>
<?php endforeach ?> 
</div>

<?php endif ?>

Error:
Call to a member function exists() on null

I dont understand because it says IF it exists… right?

The template is called ‘collection_projects’
It would be great if the page just gets all the pages with that template.

As already mentioned above, I’d also solve this with filterBy:

<?php foreach ( $site->grandChildren()->template('projects') as $child ) {
  // do stuff
}
?>
1 Like

Is children()->children() at all a valid way to go through levels of the site?
Where does the filterBy come in het example above?

Thanks a lot for your Help!!

template('projects') is a shortcut for filterBy('template', 'projects')