# Subpage from template

**URL:** <https://forum.getkirby.com/t/subpage-from-template/3082>\
**Category:** Questions\
**Tags:** v2\
**Created:** [January 19, 2016, 2:10pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082 "2016-01-19T14:10:16Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon3573533](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@anon3573533](https://forum.getkirby.com/u/anon3573533)\
**Post date:** [January 19, 2016, 2:10pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/1 "2016-01-19T14:10:16Z")

</div>

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](https://forum.getkirby.com/t/solved-how-to-fetch-content-from-subpages-with-different-templates/466/3)):

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

```

But does not show nothing.

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![pedroborges](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/pedroborges/32/7038_2.png) [@pedroborges](https://forum.getkirby.com/u/pedroborges)\
**Post date:** [January 19, 2016, 2:34pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/2 "2016-01-19T14:34:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [January 19, 2016, 2:56pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/3 "2016-01-19T14:56:20Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![anon3573533](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@anon3573533](https://forum.getkirby.com/u/anon3573533)\
**Post date:** [January 19, 2016, 3:41pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/4 "2016-01-19T15:41:26Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 19, 2016, 3:49pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/5 "2016-01-19T15:49:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon3573533](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@anon3573533](https://forum.getkirby.com/u/anon3573533)\
**Post date:** [January 19, 2016, 3:55pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/6 "2016-01-19T15:55:07Z")

</div>

Yes exactly, subpages are in differents folder:

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 19, 2016, 3:56pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/7 "2016-01-19T15:56:53Z")

</div>

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

```php
$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()`:

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

```

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [January 19, 2016, 4:06pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/8 "2016-01-19T16:06:54Z")

</div>

~~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
<?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. 😉

---

<div class="post-metadata">

**Author:** ![anon3573533](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@anon3573533](https://forum.getkirby.com/u/anon3573533)\
**Post date:** [January 19, 2016, 4:07pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/9 "2016-01-19T16:07:57Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [January 19, 2016, 4:09pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/10 "2016-01-19T16:09:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 19, 2016, 4:09pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/11 "2016-01-19T16:09:37Z")

</div>

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

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

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

```

And it worked without any problem.

---

<div class="post-metadata">

**Author:** ![anon3573533](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@anon3573533](https://forum.getkirby.com/u/anon3573533)\
**Post date:** [January 19, 2016, 4:48pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/12 "2016-01-19T16:48:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 19, 2016, 4:55pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/13 "2016-01-19T16:55:48Z")

</div>

Yes, the shorter the better:)

---

<div class="post-metadata">

**Author:** ![anon3573533](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@anon3573533](https://forum.getkirby.com/u/anon3573533)\
**Post date:** [January 19, 2016, 4:56pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/14 "2016-01-19T16:56:31Z")

</div>

As I expect, thanks !

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [January 19, 2016, 7:14pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/15 "2016-01-19T19:14:15Z")

</div>

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

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.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:00pm UTC](https://forum.getkirby.com/t/subpage-from-template/3082/16 "2025-02-06T17:00:22Z")

</div>

Archived on February 6, 2025, 5:00pm UTC


