Help with tags and tagcloud

Hi, i’ve been trying to implement tags and tagcloud for a ‘review’ blog - apologies as this seems to be a common question, but i’ve looked through the forums and haven’t been able to get any of the suggestions to work.

First off, i’ve been able to get each individual ‘review’ page to work as intended:

I have a ‘results’ sub page, and the following controller for results.php:

<?php return function($site, $pages, $page) {

  // fetch the basic set of pages
  $reviews = $site->index()->filterBy('template', 'review');;

  // add the tag filter
  if($tag = param('tag')) {
    $reviews = $reviews->filterBy('tags', $tag, ',');
  }
  return compact('reviews');
  };

And then on each ‘review’ page the tags are called like this:

<?php foreach($page->tags()->split(',') as $tag): ?>
  <ul class="tags">
     <li><a <?php ecco($tag == urlencode(param('tag')), ' class="active"') ?> href="<?php echo url('results/tag:' . $tag)?>"><?php echo $tag ?></a></li>
  </ul>
<?php endforeach ?>

What i’m now trying to do is include a tagcloud on a seperate ‘filter’ sub page, that lists all of the tags used in every ‘review’ page, and have these tags take you to the same ‘results’ page.

So my site structure is:

01.Reviews

  • (review) example 1 – tags: one, two, three
  • (review) example 2 – tags: three, four, five

02.Filter
home
results

I have the tagcloud plugin, and I can get the list of tags to work on the ‘filter’ page with the following:

<ul><?php $tagcloud = tagcloud(page('reviews'), array()) ?>
  <?php foreach($tagcloud as $tag): ?>
    <li><a href="<?php echo $tag->url() ?>"><?php echo $tag->name() ?></a></li>
  <?php endforeach ?>
</ul>

However these tags just take you to the main ‘reviews’ page, not the ‘results’ page. Is there anyway of changing where these tags link to from the ‘filter’ sub page? Or any other way of controlling where these tags take you?

Cheers.

:slight_smile:

Try to create the url in the same way as in your tag example above:

<?php echo url('results/tag:' . $tag)?>

Yeah, I’ve tried:

<ul><?php $tagcloud = tagcloud(page('reviews'), array()) ?>
  <?php foreach($tagcloud as $tag): ?>
    <li><a href="<?php echo url('results/tag:' . $tag)?>"><?php echo $tag->name() ?></a></li>
  <?php endforeach ?>
</ul>

and:

<ul><?php $tagcloud = tagcloud(page('reviews'), array()) ?>
  <?php foreach($tagcloud as $tag): ?>
    <li><a href="<?php echo $tag->url('results/tag:' . $tag) ?>"><?php echo $tag->name() ?></a></li>
  <?php endforeach ?>
</ul>

and both just break the tagcloud - I no longer get any tags listed.

What error message do you get (please turn on debugging in your config.php, if you haven’t yet)?

I haven’t used the tagcloud in a while, please check what $tag returns. Maybe you have to use $tag->name() in the href attribute as well.

Edit: You can set the baseurl (i.e. the page that is used to show the results) in the options array, see the tagcloud docs. Then you can continue using $tag->url().

Thanks for your help, i’ll have a look into the array options and see if I can find a solution.

Woohoo, this worked:

<?php $tagcloud = tagcloud(page('reviews'), array('baseurl' => url('results/'))) ?>

Next question, how do I go about putting a heading on the results page e.g… Reviews tagged with [tag]? This seems like it should be easy, but I can’t for the life of me figure it out.

Thanks again for all your help.

In your controller, pass $tag to the template:

return compact('reviews', 'tag');

In your results template:

<?php if($tag): ?>
<h1>Reviews tagged with <?= $tag ?></h1>
<?php endif ?>
```

Brilliant! Thank you!