Tagcloud recipe loop error

I believe there might be an error in the “tagcloud” cookbook recipe and I can’t quite figure it out…


I’ve just copy-pasted the code & it’s outputting each tag the number of times the tag is used instead of only once:

It got even worse when I tried adding a counter for how often the tag had been used:

<?php foreach($tags as $tag): ?>
    <?php $count = $pages->children()->filterBy('tags', $tag)->count(); ?>
    <li>
      <a href="<?= url($page->url(), ['params' => ['tag' => $tag]]) ?>">
        <?= html($tag) ?> (<?= $count; ?>)
      </a>
    </li>
    <?php endforeach ?>

01

The code does make sense to me in theory, I guess it must be something obvious which I’m overlooking…

what is the outcome of var_dump($tags);?

array(9) { [0]=> string(4) “test” [1]=> string(4) “test” [2]=> string(4) “test” [3]=> string(4) “Anne” [4]=> string(4) “Anne” [5]=> string(4) “Judy” [6]=> string(5) “Hello” [7]=> string(4) “Judy” [8]=> string(5) “Hello” }

And what is the outcome of var_dump($page->children()->listed()->pluck('tags', ',', false));?

Or how is your $tags variable populated? At first glance it seems like something is wrong in your pluck statement.

Output of var_dump($page->children()->listed()->pluck('tags', ',', false));:

array(9) { [0]=> string(4) “Judy” [1]=> string(5) “Hello” [2]=> string(4) “Judy” [3]=> string(5) “Hello” [4]=> string(4) “Anne” [5]=> string(4) “Anne” [6]=> string(4) “test” [7]=> string(4) “test” [8]=> string(4) “test” }

in the page controller, following the cookbook recipe, I have

$articles = $page->children()->listed()->flip();
$tags = $articles->pluck('tags', ',', false);

Could you try: $tags = $articles->pluck('tags', ',', true); and see if it makes a difference?
What is the separator for your tags field in the text files? Is this a comma?

I changed

$tags = $articles->pluck('tags', ',', false);

to

$tags = $articles->pluck('tags', ',', true);

this seems to have done the trick…

This worked, thank you for your input! I’m wondering why it says to use false in the recipe…

:+1: As it said in the docs: https://getkirby.com/docs/cookbook/content/filtering-with-tags#tagcloud :wink:

Could you mark as solved?

1 Like

Yes, I figured it out on re-reading the whole page, but in the “blog controller” section on the same page, it says to use

 // fetch all tags
  $tags = $articles->pluck('tags', ',', false);

that’s what got me confused.

I fixed the recipe, false doesn’t make sense in this context.

1 Like