Forum
Category Filtering [Solved]
Is it possible to filter top-level categories, and either exclude or only show them?
I'm working with a list and detail set up that's filtered by category similar to the Portfolio with Categories tutorial (https://docs.grabaperch.com/video/v/portfolio-with-categories/).
I've got a set of categories set up called Archive with top-level categories Lifestyle and Beauty, each of which have sub-categories for a year.
The paths look like this:
archive archive/lifestyle archive/lifestyle/2013 archive/lifestyle/2014 etc. archive/beauty archive/beauty/2013 archive/beauty/2014 etc.
I've got an index.php page that displays one item from each of the two categories using perch_content_custom which works fine.
perch_content_custom('Past Boxes', array(
'template' => '_boxes/_past_box_list.html',
'category' => 'archive/lifestyle',
'count' => '1',
));
perch_content_custom('Past Boxes', array(
'template' => '_boxes/_past_box_list.html',
'category' => 'archive/beauty',
'count' => '1',
));
I've also got a category.php page that displays the items in a category (as per the tutorial) like so:
if (perch_get('cat')) {
perch_category(perch_get('cat'),array(
'template'=>'category_single_pastboxes.html',
));
perch_content_custom('Past Boxes', array(
'template' => '_boxes/_past_box_list.html',
'page' => '/past-boxes/index.php',
'count' => '12',
));
} else {
perch_categories(array(
'set' => 'archive',
));
}
This also works fine (everything I expect to display is displayed), but I'd like to be able to sort the list differently depending on the category using 'sort-order' => 'DESC', or 'sort-order' => 'ASC'.
Ideally I'd like to sort anything in the /archive/beauty or /archive/lifestyle to DESC but have the content output in the sub-categories /archive/beauty/2013 and /archive/lifestyle/2013 etc by ASC
I thought I could do this using two versions perch_content_custom() to show only the top categories and sort that, and then show everything but the top categories and filter that the other way.
I've tried the code below (hoping that !archive/lifestyle would only prevent the top-level), but this filters out the sub-categories too.
if (perch_get('cat')) {
perch_category(perch_get('cat'),array(
'template'=>'category_single_pastboxes.html',
));
perch_content_custom('Past Boxes', array(
'template' => '_boxes/_past_box_list.html',
'page' => '/past-boxes/index.php',
'category' => array(perch_get('cat'), '!archive/lifestyle'),
'count' => '12',
'sort' => '_ID',
'sort-order' => 'DESC',
));
perch_content_custom('Past Boxes', array(
'template' => '_boxes/_past_box_list.html',
'page' => '/past-boxes/index.php',
'category' => array(perch_get('cat'), '!archive/beauty'),
'count' => '12',
'sort' => '_ID',
'sort-order' => 'DESC',
));
} else {
perch_categories(array(
'set' => 'archive',
));
}
Alternatively, if I can't do that I'd like to remove the top level categories from the navigation I have, or display them through their own template so I can remove the link on them so the top level pages wouldn't be filterable.
I'm using perch_categories() to display the navigation:
perch_categories(array(
'set' => 'archive',
'template' => 'category_nav_pastboxes.html',
));
This gives me a navigation like below.
Lifestyle 2013 2014 2015 Beauty 2013 2014 2015
I've tried using start and count for the navigation, but didn't really get anywhere with that as I would need to know how many sub-categories there is between each top level category…and that would require annual updates to that template.
Essentially I am trying to create a past box archive that will list an item for each month of a year within each category. The lifestyle or beauty top level categories should show the most recent items and DESC, while each sub-category/year should ASC and display January to December.
Is this at all possible, or have I taken the wrong approach from the start?
I've worked out how to display the category navigation and filter the top-level categories.
As above I'm using perch_categories() to display the navigation:
I've then added <perch:if id="catDepth" value="1"> to category_nav_pastboxes.html to display top-level categories without a link and add a class.
This removes the need to filter categories on category.php to display them differently as I can just remove the link in the navigation (fortunately I don't really need them in this instance).
Although it would still be nice to know how to do that (filter and sort ASC or DESC if it is possible?) …I tried to filter on both catSlug and catDepth, with match & contains but had no luck.
Anyway… hopefully the <perch:if id="catDepth" value="1"> will help anyone else that's looking to create category navigation and needs to style each level differently.
You should be able to use the
sort
andsort-order
options to sort the categories.Hi Drew, Yeah that does works, but I wanted to sort top level categories by DESC and secondary level categories by ASC and I couldn't work out how to filter them out from each other so I could do that.
My plan had been to have one perch_content_custom() filtering out the sub-categories and another filtering out the top level categories but neither 'filter' => 'catSlug' or 'filter => 'catDepth' seemed to work for me.
I don't think it's possible to sort in two directions with one query, unfortunately.
No I didn't think so, but I thought I could workaround it by having two instances of perch_content_custom() on one page.
However I've just realised (using <perch:all />) that I was trying to filter by a region that wasn't available to filter by. My bad! I'll leave this here so others can learn from my stupidity.
The perch:if within the navigation template above solves my problem though!