user2428795
user2428795

Reputation: 547

Make a list of posts with a tag in Markdown and Jekyll

How do I make a list of posts with a tag in Markdown and Jekyll? What I am trying to find is some how to change the following code:

<ul class="posts">
{% for post in site.posts limit: 20 %}
  <div class="post_info">
    <li>
         <a href="{{ post.url }}">{{ post.title }}</a> 
         <span>({{ post.date | date:"%Y-%m-%d" }})</span>
    </li>
    </div>
  {% endfor %}
</ul>

To only show posts with the tag "question"? Can this be done?

Upvotes: 3

Views: 3299

Answers (2)

drewhop
drewhop

Reputation: 11

Here's another variation:

<ul class="posts">
{% assign count = 0 %}
{% for post in site.posts %}
  {% if post.tags contains 'question' %}
    {% if count < 20 %}
      {% assign count = count|plus:1 %}
      <div class="post_info">
        <li>
          <a href="{{ post.url }}">{{ post.title }}</a>
          <span>({{ post.date | date:"%Y-%m-%d" }})</span>
        </li>
      </div>
    {% endif %}
  {% endif %}
{% endfor %}
</ul>

Upvotes: 1

Carlos Agarie
Carlos Agarie

Reputation: 4002

user1177636's method should work, but there's a simpler one:

<ul class="posts">
{% for post in site.tags.question limit: 20 %}
  <div class="post_info">
    <li>
         <a href="{{ post.url }}">{{ post.title }}</a>
         <span>({{ post.date | date:"%Y-%m-%d" }})</span>
    </li>
    </div>
  {% endfor %}
</ul>

This way, Jekyll does most of the work for you. :)

Upvotes: 7

Related Questions