Answers.Body: <p>It's not the most economical way to do it but if you don't have hundreds and hundreds of entries, this should do it for you:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  use craft\elements\Entry;
  
  function getChildren($entries){
      foreach ($entries as $entry) {
          $items[] = [
              'title' =&gt; $entry-&gt;title,
              'slug' =&gt; $entry-&gt;slug,
              'level' =&gt; $entry-&gt;level,
              'children' =&gt; $entry-&gt;hasDescendants ? getChildren($entry-&gt;children) : false
          ];
      }
      return $items;
  }
  
  return [
      'endpoints' =&gt; [
          'structure.json' =&gt; function() {
              return [
                  'elementType' =&gt; Entry::class,
                  'criteria' =&gt; [
                      'section' =&gt; 'structure',
                      'orderBy' =&gt; 'lft asc',
                      'level' =&gt; 1,
                      'limit' =&gt; null
                  ],
                  'transformer' =&gt; function(Entry $entry) {
                      return [
                          'title' =&gt; $entry-&gt;title,
                          'slug' =&gt; $entry-&gt;slug,
                          'level' =&gt; $entry-&gt;level,
                          'children' =&gt; $entry-&gt;hasDescendants ? getChildren($entry-&gt;children) : false
                      ];
                  },
              ];
          }
      ]
  ];
  
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-27T09:12:41.403Z
Answers.Text: Oli - you're a legend! Thank you so much for helping with this. Apologies for not getting back to you sooner.
Answers.UserDisplayName: Jim Pannell
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-14T15:52:02.117Z
Answers.LastActivityDate: 2019-10-14T15:52:02.117Z
Answers.UserDisplayName: Oli
Body: <p>I'm experimenting with using Craft in a headless way and am successfully using the Element API to return all the data required to show individual pages.</p>
  
  <p>However, to be able to show sub navigation and where a particular page fits into my site's 'structure' hierachy, I'd like to pull out the entire page hierarchy and then use that info in my application.</p>
  
  <p>Does anyone know how to go about that using the Element API?</p>
  
  <p>Thanks in advance for any clues.</p>
  
  <p>Cheers,</p>
  
  <p>Jim</p>
  
Tags: <craft3><plugin-elementapi>
Title: How to return a hierarchical list of a site's page structure using the ElementAPI
ThreadText: # How to return a hierarchical list of a site's page structure using the ElementAPI
  
  (Asked by: username_0 on 2019-10-10)
  
  I'm experimenting with using Craft in a headless way and am successfully using the Element API to return all the data required to show individual pages.
  
  However, to be able to show sub navigation and where a particular page fits into my site's 'structure' hierachy, I'd like to pull out the entire page hierarchy and then use that info in my application.
  
  Does anyone know how to go about that using the Element API?
  
  Thanks in advance for any clues.
  
  Cheers,
  
  Jim
  
  **Tags:** craft3, plugin-elementapi
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-10-14. Score: 2 | Accepted answer
  
  It's not the most economical way to do it but if you don't have hundreds and hundreds of entries, this should do it for you:
  
  ```php
  <?php
  use craft\elements\Entry;
  
  function getChildren($entries){
      foreach ($entries as $entry) {
          $items[] = [
              'title' => $entry->title,
              'slug' => $entry->slug,
              'level' => $entry->level,
              'children' => $entry->hasDescendants ? getChildren($entry->children) : false
          ];
      }
      return $items;
  }
  
  return [
      'endpoints' => [
          'structure.json' => function() {
              return [
                  'elementType' => Entry::class,
                  'criteria' => [
                      'section' => 'structure',
                      'orderBy' => 'lft asc',
                      'level' => 1,
                      'limit' => null
                  ],
                  'transformer' => function(Entry $entry) {
                      return [
                          'title' => $entry->title,
                          'slug' => $entry->slug,
                          'level' => $entry->level,
                          'children' => $entry->hasDescendants ? getChildren($entry->children) : false
                      ];
                  },
              ];
          }
      ]
  ];
  
  ```
  
  > **Comments:**
  > **username_0**: Oli - you're a legend! Thank you so much for helping with this. Apologies for not getting back to you sooner.  
  
  ---
  
  