Using tags to filter feed, add a title and URL

Hi all,

I currently have a feed of photos, and I would like to use tags to dictate their URL, add a page title and then define what photos show (based on the tag used).

My code for the correct page title in my photos.php template is:

<?php if(param('tag')): ?>
      <h2 class="fadeIn h1 m0 mb3 p0 tag"><?php $tag = param('tag'); echo $tag  ?><h2>
      <?php else: ?>
      <h2 class="fadeIn h1 m0 mb3 p0 tag"><?php echo $page->title() ?></h2>
<?php endif ?>

The code I’m using for filtering by tag is:

<?php $projects = page('photos')->children()->visible()->filterBy('tags', param('tag'), ',')->sortBy('date', 'desc'); ?>
      <div class="feed relative z2">
      <?php foreach($projects as $project): ?>
        <article class="mb3">
          <section class="flex flex-wrap items-center">
          <?php foreach($project->images()->sortBy('sort', 'asc') as $image): ?>
            <figure class="m0 p0">
              <img class="b-lazy block max-width" src="<?= $image->url() ?>" alt="<?= $page->title()->html() ?>" />
            </figure>
          <?php endforeach ?>
          </section>
        </article>
<?php endforeach ?>

I can’t, however, get the URL slug to remove the tag: and I’d like to remove the parent folder from the URL too.

Cheers!

You can use routes to get rid of the parent folder.

You can also use a route to get rid of the tag parameter, then instead of using the parameter, you would do the filtering within the route’s action. The URL must contain the tag as a slug then, though. See https://getkirby.com/docs/developer-guide/advanced/routing#actions, “Returning a page with additional data for the template”

Thanks mate.
Getting rid of the parent folder works fine using the method listed, however, trying to implement the date in URLs as per https://getkirby.com/docs/developer-guide/advanced/routing#simulating-wordpress-urls doesn’t seem to work at all?

c::set('routes', array(
  array(
    'pattern' => '(:num)/(:num)/(:num)/(:any)',
    'action'  => function($year, $month, $day, $uid) {

      // search for the article
      $page = page('posts/' . $uid);

      // redirect to the article or the error page
      go($page ? $page->url() : 'error');

    }
  )
));

Are you using multiple c::set() rules, maybe? Multiple routes must be part of the same c::set() routes array.

Ahh yeah, good call. Thanks for that.