Tagcloud with active class on current tag?

Hi all,

How would I give the current opened tag in the url (/tag:) an active class in the tagcloud?

Here’s how my tagcloud code looks right now:

<?php $tags = $page->images()->pluck('tags', ',', true); ?>

<ul>
  <?php foreach($tags as $tag): ?>
  <li>
    <a href="<?php echo url('work/tag:' . $tag)?>">
      <?php echo html($tag) ?>
    </a>
  </li>
  <?php endforeach ?>
</ul>

Any ideas?

Hi!

What about using params() in e(); helper?

    <?php $tags = $page->images()->pluck('tags', ',', true); ?>
    <ul>
    <?php foreach($tags as $tag): ?>
      <li>
        <a href="<?php echo url('work/tag:' . $tag)?>" <? e($tag == $page->params()->tag(), ' class="red"' ?>><?php echo html($tag) ?></a>
      </li>
      <?php endforeach ?>
    </ul>

There is an example with isActive() on the helpers page, not sure if this works for tags.

<ul>
  <?php foreach($pages as $p): ?>
  <li<?php e($p->isActive(), ' class="active"') ?>>
    …
  </li>
  <?php endforeach ?>
</ul>

Thanks @andi242. Here’s what eventually worked for me:

<?php $tags = $page->images()->pluck('tags', ',', true); ?>
<?php $p = kirby()->request()->params()->tag(); ?>

<ul>
  <?php foreach($tags as $tag): ?>
  <li>
    <a <?php ecco($tag == $p, ' class="active"') ?> href="<?php echo url('work/tag:' . $tag)?>">
      <?php echo html($tag) ?>
    </a>
  </li>
  <?php endforeach ?>
</ul>

Hope this can be helpful for anyone trying to do the same. If anyone has a better solution please let me know.

Thanks!

5 Likes