# Separate tags by comma

**URL:** <https://forum.getkirby.com/t/separate-tags-by-comma/7559>\
**Category:** Questions\
**Tags:** v2\
**Created:** [May 23, 2017, 10:40am UTC](https://forum.getkirby.com/t/separate-tags-by-comma/7559 "2017-05-23T10:40:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![11bits](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/11bits/32/860_2.png) [@11bits](https://forum.getkirby.com/u/11bits)\
**Post date:** [May 23, 2017, 10:40am UTC](https://forum.getkirby.com/t/separate-tags-by-comma/7559/1 "2017-05-23T10:40:24Z")

</div>

I use this code to show tags:

```
if($tag = param('tag')) {
    $tag = urldecode(param('tag'));
    $articles = $articles->filterBy('tags', $tag, ',');
}
$tags = $articles->pluck('tags', ',', false);

<?php foreach($tags as $tag): ?>
    <a href="<?= url( url::paramsToString(['tag' => $tag])) ?>" rel="category tag"><?= $tag ?></a>
<?php endforeach ?>

```

And I’m trying to separate tags by comma using `implode`.

Any idea of how can I implement it?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 23, 2017, 10:44am UTC](https://forum.getkirby.com/t/separate-tags-by-comma/7559/2 "2017-05-23T10:44:17Z")

</div>

Hm, I don’t quite see where imploding tags would fit in the above context?

If you have an array of tags, you can of course implode it:

```auto
$tags = $articles->pluck('tags', ',', false);
$list = implode(',', $tags);
echo $list;

```

---

<div class="post-metadata">

**Author:** ![11bits](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/11bits/32/860_2.png) [@11bits](https://forum.getkirby.com/u/11bits)\
**Post date:** [May 23, 2017, 10:55am UTC](https://forum.getkirby.com/t/separate-tags-by-comma/7559/3 "2017-05-23T10:55:30Z")

</div>

Well, the idea is to show tags separate by comma.

I see that using `<?= implode(', ', $tags); ?>` I get the list of tags separate by comma, but I don’t know how to include it in the foreach loop.

Or maybe there is a simpler way to do it.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 23, 2017, 11:02am UTC](https://forum.getkirby.com/t/separate-tags-by-comma/7559/4 "2017-05-23T11:02:23Z")

</div>

That’s what I meant, it does not make sense to include it in the foreach loop, otherwise you get a comma separated list of tags for each loop, but no links. But since you want a link around each tag, a simple comma separated list of tags does not make sense? In the example above, I would include the comma via CSS.

```auto
a[rel~="category"]:not(:last-child):after {
  content: ', ';
}

```

---

<div class="post-metadata">

**Author:** ![11bits](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/11bits/32/860_2.png) [@11bits](https://forum.getkirby.com/u/11bits)\
**Post date:** [May 23, 2017, 11:32am UTC](https://forum.getkirby.com/t/separate-tags-by-comma/7559/5 "2017-05-23T11:32:34Z")

</div>

Yes, I was looking for a php method to do it but your CSS solution is simple and practical.

Thanks

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 25, 2017, 12:17am UTC](https://forum.getkirby.com/t/separate-tags-by-comma/7559/6 "2017-05-25T00:17:29Z")

</div>

For completeness sake, here is a PHP version:

```auto
<?php
$tags = page('projects')->children()->pluck('tags', ',', false);

function wrapMe($tag)
{
  return '<a href="' . url( url::paramsToString(['tag' => $tag])) . '" rel="category tag">' . $tag . '</a>';
}

$tags = array_map("wrapMe", $tags);

echo implode(',', $tags);
?>

```

**I’d strongly recommend using the CSS solution, though.**

---

<div class="post-metadata">

**Author:** ![11bits](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/11bits/32/860_2.png) [@11bits](https://forum.getkirby.com/u/11bits)\
**Post date:** [May 25, 2017, 7:54am UTC](https://forum.getkirby.com/t/separate-tags-by-comma/7559/7 "2017-05-25T07:54:32Z")

</div>

Yes, the CSS solution is better.

And it makes sense, after all it’s a matter of presentation.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 26, 2017, 6:30pm UTC](https://forum.getkirby.com/t/separate-tags-by-comma/7559/9 "2017-05-26T18:30:56Z")

</div>

Well, maybe, but the question is if the drop in performance is really noticeable, compared to all the other stuff.

The point here was to use a selector that already existed in the DOM instead of introducing another selector, i.e. a new class.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:49pm UTC](https://forum.getkirby.com/t/separate-tags-by-comma/7559/11 "2025-02-06T16:49:54Z")

</div>


