Forum
Category Filtering in Template
Hello,
I was wondering if it's possible to filter a categories field by level within a template?
Here's what I'm using in my template:
<perch:categories id="dept" label="Department(s)" set="departments" />
For my Departments set, I have something like the following structure:
- Academics: Mathematics, Science, History
- Educational Support: Counselors, Health Services, Library
- Athletics: Soccer, Basketball, Baseball
When I go to edit a region item, my department's field lists all categories and sub-categories - which is nice. However, I wonder if there's a way I can list the 1st level of categories without their subcategories, for example:
- Academics
- Educational Support
- Athletics
I know I could just create another category set for each 1st level category. But I like the idea of keeping things organized under my Departments category set.
Any ideas?
You should be able to filter for
catDepth
in your template.Thanks Drew, exactly what I was looking for.
I have a question regarding Runway and routes for this, too.
In my blog I have the following route setup and it works well for my blog archives page:
How would I setup a route for subcategories of my blog categories?
For example, I have a set named Blog. The above route works fine for a category of "Athletics" in my set. However, if I add a sub-category to Athletics of "Baseball", I get no results. Baseball becomes another item listed under my categories navigation for my page, but when I select it, the archive page displays with no results and the URL I see is "mysite.com/blog/category/baseball".
You need to use what we call the category path which is a path made up of the set slug and then the category slugs:
Use that in your URL, and in your route pattern, you'll need to add entries to match the URL as it gets progressively longer. (We have a more elegant solution for that coming in Perch 2.9)
Thanks Drew. Very much looking forward to the 2.9 updates.
Ok, so I did this in my
category_link.html
template:I used the replace attribute tag because my url was displaying as
mysite.com/blog/category/blog/athletics/baseball
. So now I'm getting the URL I want:mysite.com/blog/category/athletics/baseball
.I also added this route pattern to my Archive page:
Now all my categories are displaying results. The thing is that when I select my "baseball" category link, I get all my "athletics" posts, including my "baseball" posts. So something isn't right. Same if I select my "basketball" category - also a subcategory of Athletics. The post results include everything categorized under Athletics, so all posted categorized as:
...and so on. What am I missing or overlooking? Did I setup my new route correctly?
Is you're category set called "blog"? I thought from the example above it was "departments".
I think you'll need to show me what you're doing, otherwise I can only guess.
Drew,
Sorry for the confusion...I probably should have started another thread or should have clarified after seeing your last reply was referring to my departments category set.
I have a Departments Directory page in my website and wanted to organize the directory using categories, but I wanted to make sure that I could reuse this organization on other pages in my website and restrict on those pages, which categories in my Departments Set editors could select from. This was possible after you suggested I use catDepth filtering.
My question about setting up a route in Runway is in reference to categories of my Blog Set for the blog app. This set has categories and another level of subcategories.
The first level of categories works fine with my route setup as:
My problem is displaying results from the subcategories level. This was my original template code for my blog template of
category_link.html
:For my blog categories (category: athletics) this would display as
mysite.com/blog/category/athletics
which worked fine.However for my blog subcategories (category: athletics/baseball) this would display as
mysite.com/blog/category/baseball
which would not work.So I modified my template to the following:
I used the replace attribute to change the URL from
mysite.com/blog/category/blog/athletics
tomysite.com/blog/category/athletics
.Then I added a new route - can't say I knew 100% what I was doing here, but this seemed to work:
Now for my blog categories (category: athletics) this displays as
mysite.com/blog/category/athletics
and works fine, and for my blog subcategories (category: athletics/baseball) this displays asmysite.com/blog/category/athletics/baseball
which does display posts, but not just "baseball" posts. This displays baseball posts and all posts of the parent category "athletics" and all subcategories of athletics.I have the following categories setup in my Blog set:
Clicking on the link to any of these three displays the same posts. I could understand how clicking on
Athletics
would display posts in its category and subcategories, but clicking onAthletics/Baseball
should only display posts for baseball, not all athletics and all subcategories of athletics likeathletics/basketball
...right??Hopefully I explained it well?
Can you show me the debug output from this page?
I just finished applying a change client requested for URL to display "news" instead of "blog". So I changed the folder name to news, updated all links, templates, routes, and
post page path
under Settings. Tested and all is functioning the same.Here is the debug output from:
mysite.com/news/category/athletics/baseball
.Thanks
I can see you filtering for
athletics
but not forbaseball
. Are you usingperch_get('subcat')
in your filter at all?No, I actually was not using
perch_get('subcat')
in my archive.php file. I went ahead and modified my 'Posts by Category' section to:Unfortunately, now what I see happening is that my categories (athletics) are working, but my subcategories (athletics/baseball) are not displaying results.
I also noticed that in my category links, I see athletics listed with a
count.blog.post
value of 1. This value is correct, but I'm getting the posts for athletics and all subcategories of athletics though...a bit confusing.Here is my updated debug output:
Are these still in the
blog
set? If so:Yes, that worked! Thanks Drew!
For my
athletics
category though, I'm still getting the posts of all its subcategories included in the results.This actually makes sense and I wouldn't have a problem with it, except that the
count.blog.post
value fromathletics
is displaying1
. The value is not wrong, as I do only have 1 post categorized underathletics
, but if the results are going to include all theathletics
subcategories too, then it's confusing.The posts that are categorized in subcategories of
athletics
are not categorized in theathletics
category. So are they still being included because I'm usingcat
for both routes?Is
cat
a default or can I change it?I'm thinking if the subcategories are being included because
cat
is being used for both routes, then could I change the second route to something like this for example?It's just a label - you can change it to whatever you want.
Ok great. Thanks for all your help Drew!