I am trying to wrap my head around how to get the count of a note in the specific year to display it on the thumbnail of notes e.g. “#13 / 2024”, “#14 / 2024” etc.
I know I can count the siblings but I also need to sort by year before and then it shows Im a php noob.
Can someone help me display the # of the note in the year in the note.php snippet of the starterkit? I would probably do this in the controllers?
Thanks!
Edit: just out of curiosity it would be also nice to know how to get the # if you dont consider the year but all notes in existence. I tried this with prevlisted before but, you know, noob.
To achieve this, you can use the following section from this cookbook:
The year incl. counter is displayed in the penultimate line:
<?php
// Function that returns the formatted date
$callback = function($p) {
return $p->date()->toDate('Y');
};
// Grouping the entries with $callback
$groupedItems = page('projects')->children()->listed()->group($callback);
// Output items by year and count items
foreach($groupedItems as $year => $itemsPerYear): ?>
<?= $year .' #'. $itemsPerYear->count() ?><br>
<?php endforeach ?>
Thank you GB_DESIGN! I tried your solution but it seems there is a misunderstanding:
This gives me the count of projects/notes for each year. However I want to use a counter on the notes/projects thumbnails so I can display them in a grid view with instead of the date, the # of project it was that year.
So I would need something like “Project 2 of 13 in 2024”, “Project 3 of 13 in 2024” …
You would still have to group them to be able to get the collections per year, then you can loop through them and get the index of each item in the collection (indexOf()).
Or you filter per year, not clear from your description, what exactly you want to achieve.