List of links to posts with a certain tag

Hello there, I’m an absolute newbie with Kirby 2 (or PHP for that matter). I’m trying to do the following but have no idea how to proceed: I have a “Projects” page where I’d like to have two lists, one with links to projects tagged with “tag1” and another with links to projects tagged with “tag2”. That’s probably not very hard to do but I’d be grateful for some help :slight_smile:
Thanks in advance!

Benoit

You can create two filtered collections:

<?php
$projectsWithTag1 = $page->children()->visible()->filterBy('tags', 'tag1', ',');
$projectsWithTag2 = $page->children()->visible()->filterBy('tags', 'tag2', ',');
?>

Then loop through each collection

<?php foreach ($projectsWithTag1 as $project): ?>
<!-- HTML for list 1 -->
<?php endforeach ?>

Same for the second list.

There are other ways to achieve that, e.g. using groupBy()(with a field that has only one value) or group (for more complex stuff and if the field contains more than one value, e.g comma separated list of tags): http://k2.getkirby.com/docs/cookbook/sorting-and-grouping

1 Like

Thank you so much @texnixe, I will try that!

It works perfectly, thanks again! :smiley: