bruno
April 20, 2019, 9:25am
1
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 ?>
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);
?
bruno
April 20, 2019, 10:21am
3
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.
bruno
April 20, 2019, 10:25am
5
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?
bruno
April 20, 2019, 10:32am
7
I changed
$tags = $articles->pluck('tags', ',', false);
to
$tags = $articles->pluck('tags', ',', true);
this seems to have done the trick…
bruno
April 20, 2019, 10:33am
8
This worked, thank you for your input! I’m wondering why it says to use false
in the recipe…
bruno
April 20, 2019, 10:38am
10
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