Forum

Thread tagged as: Problem

I do not understand categories

Getting a bit frustrated with this — I've always found this task a breeze in vanilla php(wasting so much time).

I have a two page listing/detail setup.

I have created a set called 'Machines', with various categories 'Turning', 'Milling' etc.

On my detail template at the top I have

<perch:categories id="categories" label="Machine Types" set="Machines" required="true" />

The field 'Machine Types' shows up and I am able to select a category for that item.

In my machines.php page (equivalent to products.php) I cannot filter by category.

So for instance as a test

                perch_content_custom('Machines', array(
                    'template' => 'machine_listing.html',
                    'category' => 'Turning'
                ));

Comes up with nothing, albeit I have a product with that category set

I've tried using the same 'category' filter on my 'machine.php' page and again nothing.

If I go to /machines.php/turning/ perch_get('cat') returns bool(false)

Please what am I missing? I'm not finding the docs very intuitive.

Russell Gooday

Russell Gooday 0 points

  • 4 years ago

trial and error /machines.php/?cat=machines/turning/ worked!!

There's a better way though?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Breaking it down, perch_get() is basically the same as $_GET with some error checking built in. So perch_get('cat') is like using $_GET['cat']. For it to return anything you need to have cat=something on the query string.

Secondly, the category option filters on a category path. The path is like a file path, but is made up of the slugs of the category set, followed by the category within the set.

So for a set called Machines, the set slug would be machines. A category within that is Turing with a slug of turning. So the category path for that category would be

/machines/turning

Thank you Drew, it's starting to make more sense. So my initial mistake during testing was to use 'category' => 'turning', when it should have been 'category' => 'machines/turning'.

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's right.