Best practice for showing catalog

Thanks for the encouragement. I think for now I’ll stick to just tags. Can i be a pain and get en example of how to use tags in my dropdown instead of child pages.

My menu code is

  <ul class="nav navbar-nav">
    <?php foreach($pages->visible() as $item): ?>
    <li class="menu-item<?= r($item->isOpen(), ' is-active')?><?php e($item->hasChildren(), ' dropdown')?>">
      <a id="<?= $item->title()->html() ?>" href="<?= $item->url() ?>" <?php e($item->hasChildren(), ' class="dropdown-toggle disabled" data-toggle="dropdown"')?>><?= $item->title()->html() ?></a>
        <?php if($item->hasChildren()): ?>
          <ul class="dropdown-menu dropdown-menu-left" role="menu">
            <?php foreach($item->children() as $child): ?>
              <li><a href="<?= $child->url() ?>"><?php echo $child->title() ?></a></li>
            <?php endforeach ?>
          </ul>
        <?php endif ?>
    </li>
    <?php endforeach ?>
  </ul>

I’ve tried using the pluck method below but no luck:

<ul>
   <?php
     $tags = $page->children()->visible()->pluck('tags', ',', true);
     foreach($tags as $tag):
   ?>
      <li><a href="#"><?php echo $tag ?></a></li>
   <?php endforeach ?>
</ul>
1 Like

You probably have to use $tags = $item->children()->visible()->pluck('tags', ',', true); in the context of your navigation?

1 Like

Okay but I’m a little lost how to incorporate that into my code :flushed:

And I would need some information where your tags are? I guess you don’t need a dropdown-submenu of category-tags for each item, do you?

I have only one page that has ‘children’, and those children are all linked to tags. There are no sub pages any more for any other page. So just 4 pages without children, then a 5th page that has the child articles based on tags. So essentially I don’t need to show subpages in the menu, just tags for the parent page that has them. Does that make sense?

Ok, then we just have to drop in the tags instead of the children and create a link URL with the category parameter:

<ul class="nav navbar-nav">
    <?php foreach($pages->visible() as $item): ?>
    <li class="menu-item<?= r($item->isOpen(), ' is-active')?><?php e($item->hasChildren(), ' dropdown')?>">
      <a id="<?= $item->title()->html() ?>" href="<?= $item->url() ?>" <?php e($item->hasChildren(), ' class="dropdown-toggle disabled" data-toggle="dropdown"')?>><?= $item->title()->html() ?></a>
        <?php if($item->hasChildren()): ?>
          <ul class="dropdown-menu dropdown-menu-left" role="menu">
            <?php foreach($item->children()->pluck('tags', ',', true) as $tag): ?>
              <li><a href="<?= $item->url() . '/category:' . $tag ?>"><?php echo $tag ?></a></li>
            <?php endforeach ?>
          </ul>
        <?php endif ?>
    </li>
    <?php endforeach ?>
  </ul>

Thanks - the menu is now displaying fine. But the the url of the tags (http://site.com/products/category:mowers)

Just takes me to the products parent template page, and not of the specific products for that tag - why would the be?

(There is no difference in the results from the following 2 urls:
http://site.com/products/category:mowers
http://site.com/products/)

In your controller or template for the $item page, you would then filter by that parameter:

<?php

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


  $children = $page->children()->visible();
  if($category = param('category') {
   // filter by category (or whatever the name of the field in your blueprint
   $children = $page->children()->visible()->filterBy('category', $category);
  }
  return compact('children');

};

Ok, maybe that was the issue. Can I just put it at the start of my product template?

Not as it is, just the inner bit:

$children = $page->children()->visible();
  if($category = param('category')) {
   // filter by category (or whatever the name of the field in your blueprint
   $children = $page->children()->visible()->filterBy('category', $category);
  }

But it should go into the products template, not the individual product…

By the way, if you use checkboxes instead of the tags field (or a mulit-select field), all the code would be the same. Checkboxes are stored in the same way in the content file as tags, that is as a comma separated string.

Hmm am getting a syntax error on line 3:

if($category = param('category') {

‘syntax error, unexpected ‘{’’

Oh, yes, a missing closing parenthesis again, so sorry, happens to me all the time…

if($category = param('category')) {

(no, I only add these little errors to test your coding skills :innocent:)

Happens even to the best :wink:

Still hasn’t changed anything - just a bit confused how this would work though because I’m using tags, not categories.

Have you adapted the code, so that you use the right field name?

I’ve used ‘tags’

<?php
$children = $page->children()->visible();
  if($category = param('category')) {
   // filter by category (or whatever the name of the field in your blueprint
   $children = $page->children()->visible()->filterBy('tags', $category);
  }
?> 

Blueprint is:

title: Product

pages: false
files:
  sortable: true
fields:
  title:
    label: Title
    type: title
  text:
    label: Text
    type:  textarea
  tags:
    label: Tags
    type: tags
    lower: true

Sure - probably my bad use of terminology. I’m not using categories at all. I was simply making use of tags in order to categorise the articles and give me a hook that I could use to filter the articles. It’s as simple as that.

Do you happen to be on Windows where a colon in the URL does not work maybe?

No I’m on a mac! Is there something specifically I should check?

Are you using tags with spaces maybe?

If you can provide your stuff for download as .zip, I can look into this, that’s probably faster.