Making the Tag Cloud more useful

Hi, fairly new to Kirby and a PHP novice but so far really loving what Kirby is allowing me to do on my site. This has got me confused, well actually I just don’t know how to do it.

So I have my tag cloud all set up on my blog, simple one, just uses:

$tag_cloud = $page->children()->visible()->pluck('tags', ',', false);

Now that will list every tag including duplicates. Is there a way I can group all the tags to remove the duplicates, count the number of each use of a tag then return the 10 most used in the blog?

It works in my head, just not when I try to figure it out :slight_smile:

Appreciate any suggestions.

First issue, set unique to true to get only unique tags:

$tag_cloud = $page->children()->visible()->pluck('tags', ',', true);
1 Like

Apologies, it is set to true, I copied that line before I changed it.

And you still get duplicates?

No, sorry, was not clear, duplicates are not there now, so just want to return the 10 most used tags now.

Try this:

$tags = $page->children()->pluck('tags', ',');
$result = array_count_values($tags);
$tenMostUsed = array_slice($result, 0, 10);
1 Like

Thanks @texnixe, worked perfectley. When I was trying this earlier it was the count that messed things up.

Then for anyone that finds this, I just used a foreach to display, like:

<?php foreach ($tags as $key => $value): ?>
   // html here, echo the $key to get the name of the tag
<?php endforeach ?>