skip to Main Content

How can I loop through all pages in Shopify? I have tried this:

{% for page in pages %}

  {{ page.title }} <br>

{% endfor %}

It doesn’t work.

2

Answers


  1. Chosen as BEST ANSWER

    Apparently it is not possible to iterate over pages in liquid, but we can do it with JS:

    $.getJSON('/pages.json', function(data) {
        for(p in data.pages ){
             console.log( data.pages[ p ].title );
        }
    });
    

  2. You can iterate through up to 50 pages at a time with Liquid pagination:

    <ul>
    {%- paginate pages by 50 -%}
        {%- for p in pages -%}
            <li>{{ p.title }}</li>
        {%- endfor -%}
        {{ paginate | default_pagination: next: 'Older', previous: 'Newer' }} 
    {%- endpaginate -%}
    </ul>
    

    https://shopify.dev/docs/themes/liquid/reference/objects#pages

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search