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.

  • 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 Dec 3 at 1:13
  • Sorry I forgot to mention that I was using the Element API plugin for getting the entries – kayq Dec 3 at 17:22
  • This post should help achieve what you need. – Oli yesterday

Your Answer

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

Browse other questions tagged or ask your own question.