Reputation: 1078
I'm building a simple blog using Jekyll. I'm looping through all of my posts which works a treat. However, I'd like to add a number marker to each post. For example the first post would be marked with a 1, second with a 2... and so on.
My current loop likes like this:
<ol class="post-list">
{% for post in site.posts %}
<li class="post-item">
<a class="post-link" href="{{ post.url | prepend: site.baseurl }}">
<div class="post-info">
<p>Post #1</p>
<h2>{{ post.title }}</h2>
</div>
</a>
</li>
{% endfor %}
</ol>
I understand I need to add a count to this loop but I'm unsure how.
Adding the following to my loop seems to make sense:
{% for num in (1...n) %}
But I'm not sure how to use this with my existing loop.
Any help would be gratefully received.
Upvotes: 4
Views: 2701
Reputation: 2113
If you don't need to count them automatically you can simple add the variable to your posts:
---
number: 1
---
Than call it via
{{ post.number }}
Upvotes: 0
Reputation: 52819
In each liquid loop you have a counter out of the box : forloop
Change : <p>Post #1</p>
for : <p>Post #{{ forloop.index }}</p>
Upvotes: 6