Tagcloud needed

Hi all,

I am looking for something very easy - but I am unable to make it work.
I am looking for an option to reorganise a list of projects based on the categories or tags they are assigned to.

I read some articles and thought the tagcloud thing would best serve me but based on this article https://getkirby.com/docs/cookbook/tags , but I am unable to make it work.

When I implement this code, I get a list of ‘home’ and ‘page’ - which are not tags I have assigned to my projects…

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

Can anyone help me with this simple endeavour. A explanatory page is helpfull too but I have not found it yet. I have also tried the tagcloud plugin but that did not work either. Any kind help is appreciated!

$tags = $page->page('projects')->children()->visible()->pluck('tags', ',', true);

You messed up the $page->page(‘projects’). It should be $site->page(‘projects’). This would work:

<?php $tags = $site->page('projects')->children()->visible()->pluck('tags', ',', true); ?>

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

And then you could put this at the top of your projects.php template

    $projects = $page->children()
                     ->visible()
                     ->filterBy('tags', param('tag'), ',');

    foreach($projects as $project) {
       //list your $project
    }

(Edited by @texnixe).

This won’t show anything if the URL has no tag param. You need an if statement to first check if there is a tag or not.

Cool!
This worked to show all the tags assigned to projects on my homepage:

<?php $tags = $site->page('projects')->children()->visible()->pluck('tags', ',', true); ?>

Yay!

Now the second part…
So, I should put such a code as below in projects.php?
That makes sense but the url goes to mysite/blog/tag:interior… is that correct? I would presume it then needs to be pasted in blog.php or tag.php… or am I missing something? (probably :))

 <?php   
if($tag = param('tag')) {
  $projects = $site->page('projects')->children()->visible()->filterBy('tags', param('tag'), ',');
}

		
    foreach($projects as $project) {
       //list your $project
    }
?>

Correct. I’d recommend that you make each tag on the frontpage a link to http://mysite/projects/tag:interior. Then in projects.php you apply the following logic:
If tag not set -> display all projects
If tag set but not found -> display a message that says "No projects with this tag"
If tag set & found -> Display the projects with that tag

 <?php   
if($tag = param('tag')) {
  $projects = $site->page('projects')->children()->visible()->filterBy('tags', $tag), ',');
} else {
  $projects = $site->page('projects')->children()->visible();
}
		
if($projects->count()):
foreach($projects as $project) {
       //list your $project
 } 
else:
  echo "There are no projects to display";
endif;
?>

Are your tags just single words without any special characters like umlauts? Otherwise, I’d urlencode() and urldecode() them.

In the link, I wouldn’t hardcode the URL, but use the url() helper instead:

<a href="<?= url() . '/projects/tag:' . $tag ?>"><?= html($tag) ?></a>

Thanks!
Now the tags link correctfully to mysite/projects/tag:interior

However if I add that code of you guys in the projects.php the page goes blank. I am looking what could be the problem but I do not find it… I simplified the code to this and the afterwards a foreach loop is started but it goes blank…

> <?php   
> if($tag = param('tag')) {
>   $projects = page('projects')->children()->visible()->filterBy('tags', $tag), ',');
> } else {
>   $projects = page('projects')->children()->visible();
> }
> ?>

However, when I add this code below it does return a list of projects… Any ideas what could be the problem here?

<?php $projects = page('projects')->children()->visible(); ?>

There’s an extra parenthesis , should be:

$projects = page('projects')->children()->visible()->filterBy('tags', $tag, ',');

I suggest you turn on debugging in your config.php during development:

c::set('debug', true);

Rad!! Thank you so much texnixe!
I put on debugging - indeed I do not have the habit to do it and that’s why the page turned blank I see now…

So, for future notice, here is the code:

> <?php   
>  if($tag = param('tag')) {
>   $projects = page('projects')->children()->visible()->filterBy('tags', $tag, ',');
>  } else {
>    $projects = page('projects')->children()->visible();
>  }
> ?>