Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

How to filter Jekyll posts by week?

I'm building a site with weekly posts. What I would like to achieve is a home page displaying posts for the current week. Could someone steer me in the right direction?

At the moment, the closest I've gotten to this is a for loop comparing dates between posts. When a post's date is different, I can assume it was posted last week (since the site is updated on Thursdays). The problem is this is not ideal, considering that posts are published occasionally on odd days. AND, to top it off, these conditionals are not filtering posts in the category "top" when dates differ.

      {% for post in site.categories.top %}
        {% capture thisPostDate %}
          {{ post.date | date: "%m%d%Y" }}
        {% endcapture %}
        {% if thisPostDate != prevPostDate %}
          <a class="tnews-module small-onehalf medium-onethird column" href="{{ post.url }}">
            <div class="thumbnail">
              <img src="https://unsplash.it/100/100/?image=311"/>
            </div>
            <h2>{{ post.title }}</h2>
          </a>
          {% capture prevPostDate %}
            {{ thisPostDate }}
          {% endcapture %}
        {% endif %}
      {% endfor %}

Both variables are working and capturing post dates.

1 Answer

Chris McKnight
Chris McKnight
11,045 Points

I would run a partition on site.categories.top based on the year of the post and the week of the year of the post.

# %U starts weeks on Sunday
# %W starts week on Monday
site.categories.top.partition { |post| post.date.strftime('%Y-%W') }