Title for tags page

Hey folks,

I’ve gone so far down this rabbit hole that I’m starting to confuse myself, so I thought I’d just cut my losses and come here for help.

So I have my blog posts listed on my homepage, which uses the blog template. In that template, I hide the h1 title, as it’s not needed on there. However, if I then click on a tag from a post, the title is also hidden on that page too, as the filter by tag page is also using the blog template.

So what I’d like to do is add an if statement in blog.php that logically says if this is the homepage hide the h1, but if it’s a tag page show the title as “Posts tagged: [tag]”

I’ve got this far (repurposed from Starterkit)but it doesn’t work - the h1 is just hidden on both the homepage and the tag page.

<?php if (empty($tag) === false): ?>
  <h1>Posts Tagged: <?= esc($tag) ?> <a href="<?= $page->url() ?>" aria-label="All Posts">&times;</a></h1>
<?php else: ?>
    <h1 class="hidden"><?= $page->title() ?></h1>
<?php endif ?>

What am I doing wrong here?

In case you need it, here’s the code that renders the tag within the article template:

<ul class="post-meta">
    <?php foreach ($page->tags()->split() as $tag): ?>
    <li>
        <a href="<?= $page->parent()->url(['params' => ['tag' => $tag]]) ?>"><?= esc($tag) ?></a>
    </li>
    <?php endforeach ?>
</ul>

Usually, it should be something like:

if (param('tag')) {
  // do stuff
}

Maybe provide some info how you define $tag in this context, because without that information it’s not possible to say why your if statement doesn’t work.

Ooops sorry, forgot to include that. Here’s how I define it in my controller:

// add a tag filter
  if($tag = param('tag')) {
    $articles = $articles->filterBy('tags', $tag, ',');
  }

Well, yes, but what do you pass to the template in the return array?

Here’s what I have so far in the template. It kinda works, but returns all the tags on the site, not just the one that was clicked on.

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

<?php foreach($tags as $tag): ?>
    <?php if (param('tag')): ?>
        <h1>Posts Tagged: <?= esc($tag) ?> <a href="<?= $page->url() ?>" aria-label="All Posts">&times;</a></h1>
    <?php else: ?>
        <h1 class="hidden"><?= $page->title() ?></h1>
    <?php endif ?>
<?php endforeach ?>

I obviously don’t want it to loop through all the children on the site, but removing children from $tags breaks the template.

Unfortunately, that doesn’t answer my question from above. Please post the controller, thanks!

Sorry, I misunderstood. Here’s the entire controller:

<?php

return function ($page) {

  // get all articles
  $articles = $page->children()->listed()->sortBy('published', 'desc');

  // add a tag filter
  if($tag = param('tag')) {
    $articles = $articles->filterBy('tags', $tag, ',');
  }

  // add pagination
  $articles = $articles->paginate(20);

  // create a shortcut for pagination
  $pagination = $articles->pagination();

  // pass $articles and $pagination to the template
  return [
    'articles' => $articles,
    'pagination' => $pagination
  ];

};

Hm, ok, you don’t pass the $tag variable back to the template at all
So:

  return [
    'articles'   => $articles,
    'pagination' => $pagination,
    'tag'        => $tag ?? null,
  ];

The you can check

if ($tag) {
// do stuff
}

That worked perfectly. Thanks, Sonja.