Hi there,
This feels like the sort of thing I should be able to work out, but if anybody has a suggestion it would be greatly appreciated! (I’m using Kirby 2 still!)
I have an alphabetical list of people’s names but I need to output a letter for each section as a heading e.g.
A:
Rose Acorn
Adam Adams
Greg Appleby
B:
Simon Bird
Susie Bison
etc etc
Any thoughts much appreciated!
Thanks,
Rach
What you need is the group() method:
https://k2.getkirby.com/docs/cheatsheet/pages/group
Your callback would return the first letter of the lastname
Ah brilliant, thanks @pixelijn, just what I need!
Second question, sorry, is there some way to get the first letter of the lastname easily?
Where is the last name stored?
It’s in a separate lastname field
Using the example from the docs, this would return the first character of the lastname field:
$groupedItems = $page->children()->visible()->group(function($p){
return str::substr($page->lastname()->value(), 1);
});
Ah brilliant, thank you - I ended up with:
$artists = $page->children()->visible()->sortBy('lastname')->group(function($p){
$lastnamecap = str::ucfirst($p->lastname()->value());
return str::substr($lastnamecap, 0, 1);
});
Which works perfectly, thanks so much @pixelijn!