I know there have been a couple of questions about categories and there are various plugins but I’m looking for the implementation that makes most sense for my use case. So, let’s say I have a blog home page and article child pages that are associated with a single category each. On the blog home page I want to list the articles by the categories, i. e.:
Category 1
- Article
- Article
Category 2
- Article
- Article
…
…
What’s the best way to go about this? At the moment I have a multiselect field with predefined options in the article blueprint and while I guess it’s possible to filter the pages as described at Filtering with tags | Kirby CMS this will only get the pages of one category. What I need is to loop through the categories and get the posts in that category.
So, what’s the best way? Have a category structure field on the blog home page instead of a select on each article page? Setting the categories up in the config somehow (which I suppose is possible but I haven’t researched yet)? Or in a page model?
Please help! I’m overwhelmed by the possibilities.
Why do you use a multiselect field to select a single category?
But apart from that, this is easy to achieve with groupBy()
.
Right, I guess I overlooked the select field in the reference (I guess because they are structured by row and not by column).
Thanks, I didn’t think of groupBy()
. There are so many methods and properties and what not, it’s hard to know which one could even apply.
Now, groupBy works nicely to get the field value. Trying this I realized, I need additional formatting (bold/italic etc.) if I’m using the category names as headings. I suppose the select field just isn’t the right choice in that case? I guess I’ll have to resort to a structure field with a writer field for the formatted text, or do you have any better suggestion?
What do you mean with formatted text? Can you give an example? Or rather what prevents you from outputting the category name in bold or italic?
It’s a different story if you do not want to use the field value, but some text you are showing in your blueprint. In that case, there are several options, reaching from translation strings, key/value map, to accessing the blueprint: Fetching field options | Kirby CMS
I mean stuff like bold/italic (within the strings, i. e. not just global CSS formatting).
I was asking for an example…value vs expected output.
Sorry, I mean getting something like:
<h2>Heading <em>with</em> formatting</h2>
from:
$categories = $page->children()->listed()->groupBy('category');
foreach($categories as $category => $itemsPerCategory): ?>
echo('<h2>'.$category.'</h2>');
…
Where “category” is a select field defined like:
category:
type: select
options:
category1: "Category 1"
category2: "Category 2"
category3: "Category 3"
Getting the plain value isn’t enough here, obviously. I need the category text and be able to have formatting in it.
Ok, right, if there is no pattern, then you cannot do it programmatically. Which means you either create a key/value map in your config, which makes sense with non-dynamic values, or you need to indeed create a structure field with key/value pairs somewhere (i,e. in the parent page), then query the keys in your select field, and later get the formatted text from this structure field.
Thanks. Good to get the confirmation that I was on the right track.