Sorting projects (ie subpages)

I have a Kirby website with a page for projects. I started with numered folder names (1_bla, 2_bli, 3_blu…), and projects were displayed correctly in templates in the order corresponding to the numbers of the folder names.
This numbering scheme is not practical however, because I keep adding new projects (which should be displayed first in lists), so I’d have to rename all projects all the time (#1 becomes #2 and so on).

What would be a practical way of choosing the order of my ever-expanding list of projects, and how would I do that in a template, for instance in such a loop :
<?php foreach($projs as $project): ?>
?
Thank you in advance for your help.

Hi, @pimassat, welcome to the Kirby forum!

Are you creating your projects manually? Because if you do that through the Panel, the re-numbering happens automatically, once you publish a new page with a given number.

An alternative would be to number the folders by date (or add a date field) and then sort by date.

Hi @texnixe, yes I forgot to write that I do everything manually without using the panel.
I tried prepending my folder names with dates (yyyymmdd), but I got all my project in ascending order (ie oldest first), and never managed to get them sorted in descending order. I also added a “Order” field in each project file, but couldn’t sort them by this field either.

If your folders have a prepended date like this 20191003_my-folder and you do a

$page->children()->listed()->flip()

You should get them in the right order.

Also, sorting them by a field should work as well, as long as you make sure that you add the field to every page and that you don’t mess up your manually added order. Could you please post the code that didn’t work as expected?

1 Like