abbas_arezoo
abbas_arezoo

Reputation: 1078

Jekyll — Loop through posts with title and number

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

Answers (2)

Virtua Creative
Virtua Creative

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

David Jacquel
David Jacquel

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>

Documentation here.

Upvotes: 6

Related Questions