Search code examples
loopsjekyllarchive

In Jekyll how can I capture year and month outside loop?


Building an archive.html file in _layouts because Github will not allow the archive plugin I can build my file but I'm having issues trying to capture the month and year outside the loop. Typically this is done as:

<ul>
{% for post in site.posts %}
  {% assign currentdate = post.date | date: "%B %Y" %}
  {% if currentdate != date %}
    <li id="y{{currentdate}}">{{ currentdate }}</li>
    {% assign date = currentdate %} 
  {% endif %}
    <li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>

but if I want currentdate before the loop I've tried:

{% assign monthStamp = site.posts | post.date | date: "%B %Y" %}

gives me everything.

{% capture monthStamp %}{{ site.posts| post.date | date: "%Y" }}{% endcapture %}

throws error:

Liquid Warning: Liquid syntax error (line 1): Expected end_of_string but found dot in "{{ site.posts| post.date | date: "%Y" }}" in /_layouts/archive.html

In Jekyll can I get year and month for archive.html outside of the loop? In my Google kungfu search skills I'm unable to find any answers on this question or if it's been done.


Solution

  • Try doing something like below:

    {% assign latestPost = site.posts | first %}
    {% assign monthStamp = latestPost.date | date: "%B %Y" %}