Subpage from template

Hello,

How can I show subpages of a specific template. Actually I use this, witch shows subpage of wanted pages :

 <?php foreach($pages->find('projects' , 'exemple')->children()->visible() as $project): ?>
  ...
 <?php endforeach ?>

I tried this (Found here):

<?php foreach(page()->children()->filterBy('template', 'template-a') as $page): ?>
...
<?php endforeach ?>

But does not show nothing.

Thank you for your help.

Every page in Kirby has a $page variable available. Use $page->children()->filterBy('template', 'template-a') instead.

Actually the main problem is that you assign the values in the loop to the $page variable, which will break other code that thinks that this variable points to the current page.

The common practice is to use $p:

<?php foreach($page->children()->filterBy('template', 'template-a') as $p): ?>
...
<?php endforeach ?>

thank you for your help, unfortunatly both codes does not show content.

Please let me give some more informations:

I’m trying to show on homepage an excerpt of all subpages from one template page.

Thank you :wink:

You mean subpages that can be anywhere, in different subfolders? Or children of the home folder?

Yes exactly, subpages are in differents folder:

Home
  page1 (templateA)
    subpage1.1
    subpage1.2
  Page2 (templateA)
    subpage2.1
    subpage2.2

OK, should be this then: You look for all pages that have template-a and fetch their children:

$selection = $pages->filterBy('template', 'templateX')->children();

If the pages with template-a can be in other folders than those of the first level, you would have to use $site->index():

$selection = $site->index()->filterBy('template', 'template-a')->children();

This does not work as ->children() isn’t defined for the Pages object. You can however use two nested loops do get the subpages for each result:

<?php foreach($pages->filterBy('template', 'templateX') as $parent): ?>
  <?php foreach($parent->children() as $p): ?>
    ...
  <?php endforeach ?>
<?php endforeach ?>

Edit: It does actually work, so @texnixe’s code is much better. :wink:

This is exactly what I need, thank you very much.
Just to anderstand better: why do I need to keep template in filterBy('template', 'projects') to make it works ?

You can use filterBy() to filter by any field or method of the page, template is just one of them. That’s why you need to tell Kirby to filter by template.

Edit: Hm, the docs tell me something else though: https://getkirby.com/docs/cheatsheet/pages/children

Edit 3: In fact I just tested this code on the starterkit:

$selection = $pages->filterBy('template', 'projects')->children();

And it worked without any problem.

1 Like

I was writing the same message as Texnixe and both solutions works fine. Is there one “better” than the other ?

Yes, the shorter the better:)

As I expect, thanks !

Yes, after I found out that this actually works, I edited my reply above. :slightly_smiling:

Not only is @texnixe’s solution shorter, but it uses less code that could potentially break in future versions or when changing the site layout.

1 Like