# Looping Over Jekyll Collections

**URL:** https://talk.jekyllrb.com/t/looping-over-jekyll-collections/1842
**Category:** Help
**Created:** [May 27, 2018, 11:09am UTC](https://talk.jekyllrb.com/t/looping-over-jekyll-collections/1842 "2018-05-27T11:09:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![dtsdwarak](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/dtsdwarak/32/1031_2.png) [@dtsdwarak](https://talk.jekyllrb.com/u/dtsdwarak)
#### Post date: [May 27, 2018, 11:09am UTC](https://talk.jekyllrb.com/t/looping-over-jekyll-collections/1842/1 "2018-05-27T11:09:51Z")

</div>

I am trying to create a collection within my jekyll page for book reviews.

These are my changes -

```
# Jekyll collections
# Documentation - https://jekyllrb.com/docs/collections/
collections_dir: collections
collections:
    books:
      output: true
      permalink: /:collection/:title

```

And this is my folder structure for collections

```
├── collections
│ ├── books
│ │ ├── index.html
│ │ └── Harry_Potter.md
│ └── index.html
├── _config.yml

```

Upon starting the jekyll server with `jekyll serve`, I’m able to see the page directly under `http://localhost:4000/collections/books/Harry-Potter/`

But, the `index.html` file under `collections/` directory is not able to iterate over the list of books and display the pages.

Specifically,

```
  {% for collection in site.collections %}
      <section>
          <h1>{{ collection.label }}</h1>
      </section>
  {% endfor %}
</div>

```

gives the following output -

```
books
posts

```

But when I try to print `{{ site.books.size }}` as `<h1>{{ site.books.size }}</h1>` I get `0` as output.

And for the same reason,

```
{% for book in site.books %}
    <h1>{{ book.title }}</h1>
{% endfor %}

```

doesn’t generate any output at all.

Why is `{{ site.books.size }}` not able to pull up any posts?

---

<div class="post-metadata">

### Author: ![mmistakes](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/mmistakes/32/2169_2.png) [@mmistakes](https://talk.jekyllrb.com/u/mmistakes)
#### Post date: [May 27, 2018, 8:48pm UTC](https://talk.jekyllrb.com/t/looping-over-jekyll-collections/1842/2 "2018-05-27T20:48:31Z")

</div>

Try renaming your books collection folder to `_books`

Pretty sure collection directories need to start with an underscore.

---

<div class="post-metadata">

### Author: ![eric-zh](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/eric-zh/32/963_2.png) [@eric-zh](https://talk.jekyllrb.com/u/eric-zh)
#### Post date: [May 28, 2018, 8:39am UTC](https://talk.jekyllrb.com/t/looping-over-jekyll-collections/1842/3 "2018-05-28T08:39:54Z")

</div>

you may check that  
[http://talk.jekyllrb.com/t/solved-listing-pages-of-a-collection/1011](http://talk.jekyllrb.com/t/solved-listing-pages-of-a-collection/1011)

---

<div class="post-metadata">

### Author: ![dtsdwarak](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/dtsdwarak/32/1031_2.png) [@dtsdwarak](https://talk.jekyllrb.com/u/dtsdwarak)
#### Post date: [May 28, 2018, 2:03pm UTC](https://talk.jekyllrb.com/t/looping-over-jekyll-collections/1842/4 "2018-05-28T14:03:11Z")

</div>

Thanks @eric-zh!

That worked perfectly.

Now, I was using `pretty` permalinks for my posts. And I wanted to use `/:collection/:title` for my books collection.

But, I think it is messing up my post url too. My current post URL turns out to be something like  
`http://127.0.0.1:4000/_posts/2018/05/26/Setup-Samba-File-Server/` (Notice the `_posts`).

Is there a way to define category specific permalink?

```
# Permalinks
permalink: pretty
future: true

highlighter: rouge
markdown: kramdown
plugins: ['jekyll-paginate']

# Jekyll collections
# Documentation - https://jekyllrb.com/docs/collections/
collections_dir: collections
collections:
  books:
    output: true
    permalink: /:collection/:title
```

---

<div class="post-metadata">

### Author: ![dtsdwarak](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/dtsdwarak/32/1031_2.png) [@dtsdwarak](https://talk.jekyllrb.com/u/dtsdwarak)
#### Post date: [May 28, 2018, 2:04pm UTC](https://talk.jekyllrb.com/t/looping-over-jekyll-collections/1842/5 "2018-05-28T14:04:29Z")

</div>

Thanks @mmistakes! This had to be done as part of @eric-zh’s suggestion too.
