Cloudtag plugin: can't get tags for individual posts

Okay so i’ve got tagcloud plugin working fine when listing all the tags from the ‘blog’ folder/page on my blogarticle.php template (the page for the single post).

In the content folder. There is a blog folder and inside the blog folder there are individual posts folders which have the blogarticle.txt in them. The blogarticle.txt files have tags field with tags listed in them.

The code below works fine to list all the tags from all the posts. I’m calling it on the single blog post page… ‘blogarticle.php’ template

<div id="Tcomments" class="tab-pane">
                                <?php $tagcloud2 = tagcloud(page('blog'), array('limit' => 20)) ?>
                                    <div class="tag-cloud">
                                        <?php foreach($tagcloud2 as $tag): ?>
                                          <a href="<?php echo $tag->url() ?>"><?php echo $tag->name() ?></a>
                                        <?php endforeach ?>

My assumption would be that i’d just need to change page(‘blog’) to $page to get the tags for the individual post that i’m displaying or even do something like page(‘blog’)->children()->visible->nth(?) or something but i haven’t been able to get any values no matter what i try. What am i missing? My path to the individual post .txt must be wrong. Please help.

Actually, you don’t need the tagcloud:

//get an array of post tags
$tags = $page->tags()->split();
//loop through the array
foreach($tags as $tag) {
  echo $tag;

Nice! Thank you! So the tagcloud is just for compiling a list of all posts or I just don’t need to use it at all really?

The tagcloud is not really necessary because you could use the pluck() method to get all the tags of all posts; however, if I remember rightly, it has some nice methods for handling special cases like tags with spaces. Having said that, you can also handle those cases with url encoding. For fetching tags of a single post, the solution I outlined above is easier to handle, I think.

1 Like