What would be the best concept to create a list of pages (or pages in a certain directory) which have a shared category? Let’s assume the editor gives a product page a category like
----
category: animal-project (or something else)
And I want to have a page (or dropdown, or widget) where all pages with this category are displayed as a list. What would be the best option?
One. option would. be to display all subpages in that folder by default, and then. have a list of categories. or a select where. the user can select a category and then. filter the list by that category.
The same could be achieved with routes., so the routes would work like virtual subpages.
It really depends on what exactly you want to achieve. Do you want a navigation with these categories so it looks like a standard URL? Or is a URL with a parameter/query string ok?
If your. subpages can have only one category, it might make sense to sort them into category subfolders in the first place. No additional coding required, clean URLS.
Use a select field in your blueprint with a pre-determined set of categories. You can either use global fields for this so you only have one list to maintain, or get the value from a master field that is one page only.
Use the tags field and let people set the category on the fly.
As for the drop down, it’s pretty easy to populate a select field on the front end, or generate a list via filtering if you want some other kind of menu.
Thanks for all the replies. Lost my connection shortly after posting this and went home for the day. Now ready to continue.
I kind of already have a solution here: https://www.voluntourismus.de/projekte/ it is made with the static site generator lektor. The template uses this code:
There are differences of course: it is easy to get the pages from one directory, and like here to give them classes (programType gets a string from the markdown file which has to match) and solve everything with JavaScript regarding filtering. I would like to do the same with kirby, just with pages witch are in different directories and if it is tags or also classes or other stuff: I am happy as long I can do some kind of sorting/filtering without another request to the server. Still to go with tags?
Tags is just an example, you can call them whatever you like, category or whatever. Of course, you can apply JS-based sorting as well and just print the category as a class to filter by.
To get a collection from different parents, you can merge them. There are two ways to achieve this:
<?php
//Merge two collections
$exhibitions = $pages->find('exhibitions')->children()->visible()->filterB('now', 'noo');
$news = $pages->find('news')->children()->visible();
$homeflux = new Pages(array($exhibitions, $news));
?>
<?php
//Appending stuff to a collection:
$homeflux = new Pages();
$homeflux->add($pages->find('exhibitions')->children()->visible()->filterBy('now', 'noo'));
$homeflux->add( $pages->find('news')->children()->visible());
// etc...
?>
Then you can loop through the resulting collection with a foreach loop. If you want to use some sort of template. language like in. your example above, you’d have to install a plugin. By default, Kirby uses PHP, not any template language.
Thanks, I got the appendig-method to work and used the same loop as in my menu. But: I need to go one directory deeper. Do I have to use the sub sub menu code for that?
Not quite sure what you are talking about. Maybe you can post your structure and indicate what exactly. you want to fetch from where? You can. also check. the. docs for different methods like grandchildren()or index().
Yes, you can combine any pages collection into a single pages collection, but since I don’t. know. what $homeflux is and how you have defined the grandChildren, I can’t really help you with this. If you need more help, please provide the complete code.