How to target all Categories with parent ID == '#'

Hi everyone,
Relatively new to LS and trying to get my head around something.
How to target all sub-categories within a parent category within a given template.
I'm trying to achieve something akin to this:
{% if category.textpage and parent.category_id == '8661854' }
<script type="text/javascript">
// do something
</script>
{% endif %}
The only reference I can find to parent categories in the documentation is:
categories.{{ category.id }}.parent
Presumably this only returns the parent of a specific category however. Also slightly unsure how to express the ID in this statement as:
{{categories.{{ '8661854' }}.parent}}
Returns a fatal error due to the two sets of curly brackets.
If anyone could point me in the right direction here would be amazing.
Many Thanks,
Best Answers
-
Milesbd Moderator, Lightspeed Staff Posts: 31 moderator
Hi,
Thanks for the question!
If you'd like to target all child categories of a given category, you would need to construct a for loop, for the categories in the parent category.
Using your example:
{% for subs in shop.categories[8661854].subs %} <br>subcategory: {{ subs.title }} {% endfor %}
would create a loop for all subcategories of the category with a parent category id of "8661854", and output the titles, e.g:
subcategory: title1
subcategory: title2
etc.
In your example, you could do the following:
{% if category.textpage and parent.category_id == '8661854' } {% for subs in shop.categories[8661854].subs %} <script type="text/javascript"> // do something </script> {% endfor %} {% endif %}
To target the subcategories of your chosen parent category.
-
Milesbd Moderator, Lightspeed Staff Posts: 31 moderator
Hi!
Both are correct, they simply differ in the way that you are referring to the subcategory id.
For example, take this list of categories, and we ant to target 1192110:
This notation:
shop.categories.{{ category.id }}.subs
Is referring to the variable of the specific sub item of the array, like in json array notation, so when the variable is rendered, the call is expressed as:
shop.categories.1192110.subs
In the notation I supplied, I'm specifying the index position within the array of
shop.categories
for the category in question :shop.categories[1192110].subs
which still refers to
shop.categories.1192110.subs
when expressed.In answer to your question, both notations are correct, and will have the same result, as long as the
{{ category.id }}
variable is the category for which you want to target.In you're case, as you know the target that you are using for the category, the
shop.categories[8661854].subs
notation is more explicit, as you already know the ID of the category you wish to express.
Answers
Hi Miles,
Thanks, that's
exactlyalmost what I was looking for.Just to clarify the syntax?
In the rain documentation this is expressed as :
So is how you've written this above, actually the correct way?
E.g. Square brackets, no quotes around ID or full stop before ID...
Thanks,
Hi Miles,
Great explanation, appreciate the support!
Thanks