I guess I don’t understand how the structure field works. I am new to Kirby and am using version 2.4.1.
I have a structure like below.
label: Sponsor Options
label: Sponsors category
item1: Group One
item2: Group Two
item3: Group Three
item4: Group Four
item5: Group Five
label: Sponsors Name
I was trying to filter/sort the results by the option field to get an output like below:
And so on… A select option may or may not exist and I wanted to sort by the options field in descending order. I was close on several attemps but could never figure it out. I have abandoned this approach and am using a very messy blueprint. I’m sure there is an easy solution to this, but I couldn’t find it. If anyone has an idea to help create this as intended I would be appreciate it.
$pagecontent = $page->page content()->toStructure()->sortBy('category', 'desc')->groupBy('category');
foreach($pagecontent as $category => $items): ?>
<h2><?php echo $category ?></h2>
<?php foreach($items as $item) : ?>
<li><?php echo $item->title() ?></li>
<?php endforeach; ?>
<?php endforeach ?>
Note that this will output “itemX” for the category. You would have to use a category map to map the value to the desired label.
Thank you very much for the reply. This is very similar to what I had come up with before giving up on the idea. Let me try to explain better.
I was hoping to sort the data so that no matter how it was entered in the panel, the output would always be sorted by the options of the select field in descending order.
As an example, if the data is entered in the panel like
Group 5 name1
Group 4 name2
Group 5 name3
Group 1 name 4
The output would be
The sort does not work on the select field since there seems to be no way to get the elements listed in the field after the panel. Is this correct?
I’m not sure what the problem is, if you want group 1 first, you need asc order instead of descending, when I change the order in the above code to asc, my output is this:
To me, that looks like what you want, only the Group names currently missing.
Yea, you’re right. Sorry about the confusion. For some reason I got ascending and descending backwards and never switched it in the code.
Thanks for the help. (slightly embarrassed)