So, I’m creating arrays of articles from different content sections of the site. Some sections have articles broken out into grandchildren, consequently there are no ‘children’ that are articles there.
I’d like to add up all the articles throughout the site onto the homepage, but also in curated sections.
Here’s the structure:
/content/section1 // has no children
And here are the collections I’d like to create an array to include all children but not children that are sections (such as #4 or #5), just the children of those sections.
I’m thinking I need to do it this so that I can reference these article arrays in snippets as well, otherwise I’d need to redefine those
$articles arrays in the snippets, too (DRY, etc.).
A. Use Controllers — may allow merging of arrays already defined in the site controller, correct? https://getkirby.com/docs/guide/templates/controllers
B. Use Collections — each Collection file would define a collection, but could it allow merging of collections? https://getkirby.com/docs/guide/templates/collections
$articlesSec2 = page('articles2')->children()->listed();
$articlesSec3 = page('articles3')->children()->listed();
$articlesSec4 = page('grandsection/section4')->children()->listed();
$articlesSec5 = page('grandsection/section5')->children()->listed();
$articlesAll = new Pages(array($articlesSec2,$articlesSec3, $articlesSec4, $articlesSec5));
Another option would be to filter by template:
$articlesAll = pages('articles2', 'articles3', 'grandsection/section4', 'grandsection/section5')->index()->listed()->template('article');
Yes, you can create a collection that is then available from anywhere.
Brilliant. So easy. Thanks for the quick reply!
Also, can I reference a Collection inside another
Another question: Could I also just index the entire site and get all entries using the
Edit: I see it:
$articles = $site->index()->filterBy('template', 'article');
You can use
$site->index() but I hope you’ve seen the warning. It’s no problem on smaller site, but on big sites, you can run into serious performance issues.
Yes, you can fetch a collection anywhere using the
collection() helper or
Yeah, I saw the warning. There are only 58 articles for this project.
About the “Collection Helper”, I see this, but I’m not sure what this page means:
Is this creating the collection and calling it all in two lines? I’m not familiar with
so I better brush up on that.
The helper doesn’t create a collection, it just fetches it. The helper is a shortcut to using
If you follow the link to the source code under each method, e.g. here: https://getkirby.com/docs/reference/templates/helpers/collection#source, you can actually see what each method does. In the long run, this will deepen your understanding of Kirby.
dump() is a helper function, Kirby’s alternative to PHP’s
var_dump(), which in turn is simple way of inspecting a variable. Good for quick checks what you get back, not really the best way to actually debug your code.