Tag count using blog controller

Could somebody point me to the best way of outputting a tag count in a tag cloud using the blog controller described here: Filter collections by tags | Kirby CMS

I’ve seen some quite old posts on the forum but I’m unsure how to do this.

Thank you.

You would have to filter all posts by the tag in the loop to count them:

<?php

// fetch all tags
$posts = $page->children()->listed();
$tags  = $posts->pluck('tags', ',', true);

?>
<ul class="tags">
  <?php foreach($tags as $tag): ?>
  <li>
    <a href="<?= url('blog', ['params' => ['tag' => $tag]]) ?>">
      <?= html($tag) ?>(<?= $posts->filterBy('tags', $tag, ',')->count() ?>)
    </a>
  </li>
  <?php endforeach ?>
</ul>
1 Like

Thank you. That’s working now.

Should I still be using the blog controller? I think I’m duplicating what is in there?

$posts and $tags should then be defined in the blog controller

OK, got it.

Thanks again.