I’m looking to get the depth a category’s subcategories.
Suppose I have a category ‘grandparent’ which has a subcategory ‘parent’ which in turn has a subcategory ‘child’. How do I get to integer 2?
I suppose I could check if ‘grandparent’ has subcategories and if it does, check if they have subcategories, etc, until I hit 0. But that seems like a lot of unnecessary processing.
Isn’t there a more elegant way?
I would do it this way:
Altogether, it’s the following code, which is bundled into a function, but could, of course, be used directly somewhere in a template file:
Please note that I did not test this.