Loop through pages object

Highlighted
Tourist
3 0 2

Hello,

I need to retrieve all page handles, but this simple loop doesn't work:

{% for page in pages %}
  {{ page.handle }}
{% endfor %}

It seems like it 's not possible to loop the pages object, which would be an inconsistency in my opinion, but anyway:
How can I retrieve alternatively the list of all existing page handles?

 

2 Likes
Highlighted
Shopify Expert
9925 103 1763

It's not possible to iterate over pages in that way. You can however add a curated list of pages to a menu (linklist) and loop over that.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
1 Like
Highlighted
Tourist
3 0 2

Thanks Jason!

0 Likes
Highlighted
New Member
2 0 0

You can do it with JS though:

$.getJSON('/pages.json', function(data) {
    var list = "";
    for(p in data.pages ){
             list = list + data.pages[ p ].title + "\n";
    }
    console.log( list );
});

 

0 Likes
Highlighted
Astronaut
1085 177 213

Or, alternatively, fetching all pages without jQuery:

    
    async function fetchAsync () {
    let pagesUrl = `${window.location.href}/pages.json`;
    let response = await fetch(pagesUrl);

      let {pages} = await response.json();

      pages = pages.map(e => e.title);
      
      console.log(pages);
      
  }
◦ Problem Solved? ✔️ Please click on Accept as Solution and like.
◦ Hire me! Recommended by 25+ customers on Facebook. Click here to read their reviews.
◦ Contact me at: diego.boarutto.fortes@gmail.com
0 Likes