Is it possible to fetch a page by template?

Client has a field in Contacts page, that is used to show navigation link. And we also want to show this link in footer. I can’t fetch Contacts page with that field by URL, as it can change, also has different languages.

You can use filterby to pick up on the template. https://getkirby.com/docs/cookbook/content/filtering#filtering-by-methods

Note that you need to findBy('intendedTemplate', 'some-template') instead of template if the template doesn’t exist in the file system. If the user is allowed to change the template, however, finding by template is just as useless as finding by uid.

Also, I would use findBy(), not filterBy()

Working fine, thank you!

Checked with findBy(), gives error “Call to a member function maplink() on null”

  	      $contactspage = $site
  ->index()
  ->findBy('intendedTemplate', 'contact');

That’s strange, because it should work.

On a side note: Why do you search the complete index? This is not recommended if you can avoid it. And I would suspect that the page is not anywhere in the complete site tree, but usually on the first level.

Sorry, might have other code left, will check. Yes, page is on first level, will change it.

Yes, it’s working fine with findBy()

      $contactspage = $site
        ->children()
        ->findBy('intendedTemplate', 'contact');

Thanks a lot!

The difference between the two methods filterBy() and findBy() is that the first returns a Pages object, the second a single Page object. If you use filterBy() you have to get the first element (using first()) or loop through the collection.

Very nice, I still used loop with findBy() as thought it’s needed, because there can be more than one page with same template and not the only field. But yes, with findBy() works without loop, thank you!