1

How do I get all of an entry's children using the Element API?

I want to return something like:

data: [
    title: 'Root Entry'
    uri: 'root-entry'
    children: [
        {
            title: 'Level 2 Entry'
            uri: 'level-2-entry-1'
            children: [
                {
                    title: 'Level 3 Entry'
                    uri: 'level-3-entry'
                    children: [...]
                },
            ]
        },
        {
            title: 'Level 2 Entry'
            uri: 'level-2-entry-2'
            children: [...]
        },
        {
            title: 'Level 2 Entry'
            uri: 'level-2-entry-3'
            children: [...]
        },
    ]
]

I would want to get all entries up to the depth level of 4. Some entries wont have children past level 3 and some wont have any past level 2. I have tried doing a for loop traversal but cannot figure out why $entry->children returns everything but children entries.

3
  • 1
    Perhaps an obvious question, but have you tried the standard {% nav entry in entries %} loop? That should get all children and children's children.
    – 4midori
    Commented Dec 3, 2019 at 1:13
  • Sorry I forgot to mention that I was using the Element API plugin for getting the entries
    – kayq
    Commented Dec 3, 2019 at 17:22
  • This post should help achieve what you need.
    – Oli
    Commented Dec 7, 2019 at 21:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.