Single blog entry: How to display its tags?

This functionality has been broken by the latest update 2.3.0. The tags themselves are still displayed correctly, the links, however, lead to empty pages.

To be honest, the changelog overwhelms me. How am I to modify the code?

What do your links look like? The code above should not be affected by the update.

Hi, well, they look the same, with a “tag:something” suffix.

So, the links are still something like this, right, and therefore perfectly alright.

http://localhost/kirby-project/blog/tag:kirby

Have you turned on debugging in your config.php?

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

to see what sort of error you get?

Ah, it’s the obvious things I forget, thanks for the hint! Here’s what I get when I open the tag-specific page:

Fatal error: Uncaught Error: Call to a member function url() on boolean in /kirby/kirby.php:301 Stack trace: #0 /kirby/toolkit/helpers.php(270): Kirby->{closure}() #1 /kirby/kirby.php(678): call(Object(Closure), Array) #2 /index.php(16): Kirby->launch() #3 {main} thrown in /kirby/kirby.php on line 301

Is that all? That does not really tell where it is coming from …

Oh-oh. That doesn’t sound good.

Does the page display any content without the tag parameter? Do you use any page models or plugins?

Yes, it does. It displays the blog correctly, and single-article pages work as well. The only plugin I use creates the RSS feed, and there are no page models. Apart from the tag features you taught me here, there shouldn’t be anything unfamiliar.

I don’t know, is it possible that the update (via git) had a few glitches? I’m trying to re-set everything up… (Edit: Mh, didn’t work out as well.)

Some sort of caching problem, maybe?

This is the controller I use in a Kirby 2.3 starterkit and which works (but is the same as above anyway, I think)

<?php

return function($site, $pages, $page) {

  if($tag = urldecode(param('tag'))) {

    $articles = page('blog')->children()->visible()->filterBy('tags', $tag, ',')->flip();
  } else {
    $articles = page('blog')->children()->visible()->flip()->paginate(5);
  }

  $pagination = $articles->pagination();

  return compact('articles', 'pagination');
};

It’s not working I’m afraid. I reverted to Kirby 2.2.3 and it works like a charm. It has to do with something within the ’kirby’ directory.

Could you post your controller and your template, pls.?

Sure. I really appreciate you sticking around this long. Thank you.

Here’s the controller blog.php:

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

    $tag = urldecode(param('tag'));
    if(param('tag')) {
        $articles = page('blog')->children()->visible()->filterBy('tags', $tag,',')->sortBy('date')->flip();
    } else {
        $articles = page('blog')->children()->visible()->sortBy('date')->flip();
    }   
   
     return compact('articles');

};

This is the template blog.php:

<?php snippet('header') ?>
    <?php if(param('tag')): ?>
  <div class="hash"><li><?php $tag = param('tag'); echo 'Filter&#58;  &#35;' . $tag . $articles->title() ?></li><li><a href="/">Remove</a></li></div>
  <?php endif ?>


  <?php $articles = $page->children()->visible()->flip(); if($tag = param('tag')) {$articles = $articles->filterBy('tags', $tag, ',');} $articles = $articles->paginate(5); ?>
  <?php foreach($articles as $article): ?>
        <div class="post">
      <?php if(!param('tag')): ?>
      <h1 class="post-hashtag"><?php $tags = $article->tags()->split();?><?php foreach($tags as $tag): ?><a href="<?php echo url('blog/tag:' . urlencode($tag)) ?>">&#35;<?php echo $tag ?></a> <?php endforeach ?></h1>
      <?php endif ?>
            <h1 class="post-title"><a href="<?=$article->url()?>"><?=$article->title()?></a></h1>
            <?php echo $article->first()->footnotes(false)->kirbytext() ?>
            <?php echo $article->text()->footnotes(false)->kirbytext() ?>
            <div class="read-more"><a href="<?=$article->url()?>"><?=$article->date('d.m.Y')?>, <?=$article->time('G:i')?></a>

  <?php endforeach ?>

<?php if($articles->pagination()->hasPages()): ?>
        <div class="pagination">  

          <?php if($articles->pagination()->hasNextPage()): ?>
          <a class="prev" href="<?php echo $articles->pagination()->nextPageURL() ?>">&laquo; Previous</a>
          <?php endif ?>

          <?php if($articles->pagination()->hasPrevPage()): ?>
          <a class="next" href="<?php echo $articles->pagination()->prevPageURL() ?>">Next &raquo;</a>
          </div>
          <?php endif ?>

<?php endif ?>

    </div>
    </div>

<?php snippet('footer') ?>

I tested your code and it works for me. Two things, though:

  1. You duplicate the logic from the controller in the template, which does not make sense.

  2. What is first() and footnotes()?

<?php echo $article->first()->footnotes(false)->kirbytext() ?>

Seems to be simply ignored in my setup; is that a plugin that you use? Maybe that causes the issue?

Whoops, the footnotes were from a plug-in I used to test very briefly. I have removed all references, the problem still persists in 2.3.0. Sorry for that.

first() helps me to show only the first paragraph of my entry when needed. In my panel, I have a separate form field for it. first() and text() combined are the entire blog entry.

Now, your first point sounds interesting. Am I right in assuming there are redundant parts within my template?

Yes, exactly. $articles is already returned from the controller. So no need to define $articles again in the template, in this line:

<?php $articles = $page->children()->visible()->flip(); if($tag = param('tag')) {$articles = $articles->filterBy('tags', $tag, ',');} $articles = $articles->paginate(5); ?>
  <?php foreach($articles as $article): ?>
...

Not sure about that. If I remove the relevant parts from the template, it breaks its basic functionality: creating a new page after five entries and allowing the possibility to filter the blog by tags.

The controller appears to be crucial for the pages that display only one specific tag (’blog/tag:example’). If I remove it, those pages turn blank. With Kirby 2.2.3, that is! So, could it be possible that the controller is what might cause the issue on Kirby 2.3.0?

I must admit I’m short of giving up on this :confused:. If you like, you can send me your project as .zip file to sonja@getkirby.com and I’ll have a look at it.

Done. Many thanks! If we find a solution after all, I will share it, of course.

It’s solved, and it all comes down to this: While blog entries are available at /blog/this-is-the-title, tags are not if (!) the blog is set as the homepage. So, in order to filter your entries, you need to visit /tag:thisisthetag without the /blog before.