Forum
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.
trial and error /machines.php/?cat=machines/turning/ worked!!
There's a better way though?
Breaking it down,
perch_get()
is basically the same as$_GET
with some error checking built in. Soperch_get('cat')
is like using$_GET['cat']
. For it to return anything you need to havecat=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 ofturning
. So the category path for that category would beThank 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'.
That's right.