Just wondering how to only query the children on subcategories. For example...

├── Fruit
│   ├── Apples
│   ├── Bananas
│   └── Oranges
└── Vegetables
    ├── Brussels sprouts
    ├── Carrots
    └── Celery

I only want to query the children directly of fruit without having to get all of the food then have to loop around. As this isn't the best practice.

I thought it would be something like ...

{% set categories = craft.categories()
.all() %}

Thanks in advance.

  • 1
    The ancestor of expects and element or the ID of an element, not a string. You should pass the proper parameter Oct 13 '18 at 17:41

Building on Robin's comment, the ancestorOf parameter expects either an element ID or the actual element as a parameter.

For example, if you know the ID of the fruit category is 3, you'd use .anscestorOf(3), or if you already had the fruit category element, you'd do like so:

{% set fruitCategory = craft.categories.id(3).one() %}

{% set categories = craft.categories()
    .all() %}
  • Thank for getting back to me. I tried this and its still not working. So I dumped out "fruitCategory" and i was getting this returned... /home/vagrant/code/craft/vendor/twig/twig/lib/Twig/Extension/Debug.php:50:null any ideas? As a result it just returns everything Nov 4 '18 at 19:00

.ancestorOf(fruitCategory) would be going up the structure. The ancestor of Fruit in this example is Food. If you want the children of Fruit you'd use .decendantOf(fruitCategory)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.