Custom template for tags

Hello guys, i was wondering if there is a possibility to have a custom template for tags, a sort of tags archive.

I’ll try to explain better:

i have this template in site->templates->products

with a controller in controllers->products.php that fetch data this way:

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

  $products = $page->children()->visible()->flip();

  if($tag = param(‘tag’)) {
    $products = $products->filterBy(‘tags’, $tag, ‘,’);
  }
  
  $tags = $products->pluck(‘tags’, ‘,’, false);

  return compact(‘products’, ‘tags’, ‘tag’);

};

?>

In the template for products i return the products like this

<?php foreach($products as $product): ?>
  
  <section class=“products”>            
    
    <?php foreach($tags as $tag): ?>
      <h1 class=“title”><?php echo html($tag) ?></h1>  
    <?php endforeach ?>
      
    <article class=“product”>
      <h2 class=“title”><a href=“<?php echo $product->url() ?>”><?php echo $product->title()->html() ?></a></h2>
      <?php if($image = $product->image()): ?>
      <figure>        
        <?php echo thumb($image, array(‘width’ => 380, ‘height’ => 255, ‘upscale’ => true)); ?>        
      </figure>
      <?php endif ?>
    </article>
    
    <?php foreach($tags as $tag): ?>
      <a href=“<?php echo url(‘products/tag:’ . $tag)?>”> Show all </a>    
    <?php endforeach ?>

  <?php endforeach ?>
  
  </section>

I’d like to obtain a custom template that show all the products in a certain category when i click the Show all link.

Thanks everybody for the help!

You could add a filterBy() to the product loop.

<?php foreach($products->filterBy('tag', 'HandLotions') as $product): ?>

Thanks Luke, but i meant another question, i’m looking to create a template that shows me a complete list of objects from each tags

Not sure if I understand correctly, but let me have a try:

You will have to create a new folder in content, e.g. results and put a text file results.txt into it.

Next, you need to create a template results.php in templates with the code that displays your results.

Then in your product.php template, you link to the results url.

Yes texnixe, that was my question!

Hey @antoniocapuozzo I was wondering if you could share your results.php template? I haven’t quite figured out how to display the correct results…

1 Like