# Recursive nested page listing

**URL:** <https://forum.getkirby.com/t/recursive-nested-page-listing/650>\
**Category:** Plugins\
**Created:** [March 31, 2015, 3:08am UTC](https://forum.getkirby.com/t/recursive-nested-page-listing/650 "2015-03-31T03:08:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samnabi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/samnabi/32/5535_2.png) [@samnabi](https://forum.getkirby.com/u/samnabi)\
**Post date:** [March 31, 2015, 3:08am UTC](https://forum.getkirby.com/t/recursive-nested-page-listing/650/1 "2015-03-31T03:08:29Z")

</div>

Here’s a plugin I made to display a nested HTML list for all a page’s descendents, no matter how many levels deep it goes.

**showIndex.php**

```php
    /**
      Function to create a nested list in HTML from Kirby's index()
      @param must be an index() object
      @return string
    */
    function showIndex($pages) {
      
      // Initialize output string
      $output = '';
    
      // Get the initial nesting level
      $inital_level = substr_count($pages->data[0],'/');
    
      // Loop through pages
      foreach ($pages->data as $page) {
    
        // Set the page's indent level. Markdown requires 4 spaces for each level of depth.
        $prefix = str_repeat(' ', $page->depth() - $intial_level - 2);
    
        // Manually build the output string in kirbytext/markdown format
        $output .= $prefix.'- (link: '.$page->url().' text:'.$page->title().")\n";
      }
    
      // Return the list as HTML
      return kirbytext($output);
    }

```

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [March 31, 2015, 8:14am UTC](https://forum.getkirby.com/t/recursive-nested-page-listing/650/2 "2015-03-31T08:14:58Z")

</div>

Have a look at `$page->index()`: [http://getkirby.com/docs/cheatsheet/page/index](http://getkirby.com/docs/cheatsheet/page/index)  
And `$page->depth()`: [http://getkirby.com/docs/cheatsheet/page/depth](http://getkirby.com/docs/cheatsheet/page/depth)

---

<div class="post-metadata">

**Author:** ![samnabi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/samnabi/32/5535_2.png) [@samnabi](https://forum.getkirby.com/u/samnabi)\
**Post date:** [March 31, 2015, 11:46am UTC](https://forum.getkirby.com/t/recursive-nested-page-listing/650/3 "2015-03-31T11:46:06Z")

</div>

Nice, I didn’t know about `depth()`. That simplifies some of the code but I still need to use `substr_count()` to get `$initial_level` since it’s working with a string, not an object.

Code updated above.

---

<div class="post-metadata">

**Author:** ![samnabi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/samnabi/32/5535_2.png) [@samnabi](https://forum.getkirby.com/u/samnabi)\
**Post date:** [March 31, 2015, 11:48am UTC](https://forum.getkirby.com/t/recursive-nested-page-listing/650/4 "2015-03-31T11:48:35Z")

</div>

There’s still a hacky offset required to define `$prefix`, so if anyone has ideas for improvement I’m all ears.
