How to list subpages

How to list subpages of current page, but not an infinite list but depth n, so that I don’t have to hard code a list of subpages from settings.


{% for subpage in page.subpages | depth: 1 %}
  {{ subpage.title }}
{% endfor %}

Where page is not necessarily a top level page.

If you’re limited to Liquid, then one approach would be to filter site.pages based on URL. Something like the following would produce a list of all subpages (untested code with many assumptions):

{% site.pages | where_exp: "p", "p.url contains page.dir" | inspect %}

Depth filtering could then be done counting slashes (i.e. use split on a page’s url and get list-length).

1 Like

Thank you.

I had more or less the same idea. The downside is that it lists an infinite number of subpages regardless of depth. So I’m filtering the results by splitting the url by slashes to list only first level children.

I would be very surprised if this is the best approach and if there is no built-in {% page.children %} method.

Hi, you’re in luck! I’ve built a demo project around this direct descendants idea.

Check my list-pages.html to get pages in the same folder as the current page.

And list-sections.html to get directories in the current directory. Preview here.

    {% for item in site.pages %}
        {% assign item_crumbs = item.url | remove_first: page.dir | split: '/' %}

        {% if item_crumbs.size == 1 and == '' %}
            <a href="{{ item.url | relative_url }}">{{ item.title }}</a>
        {% endif %}
    {% endfor %}

Covered in _includes directory of Nested Jekyll Menus.

See my comments in those files or look at for more info.


Using for and if is like using where_expr but the logic I did is easier over multiple lines esp for the assign.

I treat pages and directories (just in a dir) as separate things across two includes files but I guess you can combine them.

Thank you. It works.
I’m surprised there is no built-in method for this.