This question already has answers here:
Get Jekyll Posts by Year [duplicate]
(3 answers)
Closed 8 years ago.
I'm making an "archive" if you will of all the posts on my site. I want to gather all the posts from a year. This code works fine, however; I want it to generate the <h2>2014</h2> when needed.
Basically, if year is 2014, render <h2>2014</h2> and make a <ul> of all the posts (with category of journal) from that given year.
If anyone knows of any .rb plugins that archive by year, let me know!
<h2>2014</h2>
{% for post in site.categories.journal %}
{% capture year %}{{post.date | date: "%Y"}}{% endcapture %}
{% if year == "2014" %}
<ul class="posts-in-year">
<li><p>{{ post.title }} — {{ post.date | date: "%B %d" }}</p></li>
</ul>
{% endif %}
{% endfor %}
I want it to look like this:
{% for post in site.categories.journal %}
{% capture currentyear %}{{post.date | date: "%Y"}}{% endcapture %}
{% if currentyear != year %}
<h2>{{ currentyear }}</h2>
{% capture year %}{{currentyear}}{% endcapture %}
{% endif %}
<ul class="posts-in-year">
<li><p>{{ post.title }} — {{ post.date | date: "%B %d" }}</p></li>
</ul>
{% endfor %}
Related
I am creating an archive by date and using this solution.
I want to add the category of the post next to the post and am having trouble capturing the category name of a post since categories are indexed through the site
{% else %}
<li>{{ post.title }}
{% capture category_names %}{{ ## need to capture category }}{% endcapture %}
{% if category_name %} - {% for category in category_names %}
{{category}}
{% unless forloop.last %} {% endunless %}
{% endfor %}
{% endif %}
{% if post.date %} - {{ post.date | date: "%m/%d/%Y" }}{% endif %}</li>
{% endif %}
Currently the code above creates {post name } - - mm/dd/YYYY as I want without the category name in between the 2 '-'.
Any idea how to capture the category name of a post? Thank you.
I think I'm not even sure what I should be searching for. On my Jekyll Blog I currently have just blog posts that have been written and listed with their full content on the main page and using pagination. I want to follow more Indie Web standards and follow a PESOS (Publish Elsewhere, Syndicate (to your) Own Site) method. I've figured out a way to get data from my Twitter (e.g. post_date, embed code, etc) into a YAML data file automatically. What I want to do is take the data from my posts and combine the data from Twitter and include those posts as if they were also blog posts (the plan is to do the same with Instagram as well).
I've tried a lot of things, but I'm not even sure what is the best way to go about doing this. I'm assuming that it will use something similar to Using Jekyll, how do you alter an array's contents using a for loop?, but I can't seem to make it work. My code for the blog posts is as follows currently:
{% for post in paginator.posts %}
{% if post.header.teaser %}
{% capture teaser %}{{ post.header.teaser }}{% endcapture %}
{% else %}
{% assign teaser = site.teaser %}
{% endif %}
{% if post.id %}
{% assign title = post.title | markdownify | remove: "<p>" | remove: "</p>" %}
{% else %}
{% assign title = post.title %}
{% endif %}
<div class="list__item">
<article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">
<h1 class="archive__item-title" itemprop="headline">
{% if post.link %}
{{ title }} <i class="fas fa-link" aria-hidden="true" title="permalink"></i><span class="sr-only">Permalink</span>
{% else %}
{{ title }}
{% endif %}
</h1>
<p>Posted on {{ post.date | date: "%A %B %-d, %Y" }} by Jacob Campbell.</p>
{{ post.content }}
</article>
</div>
{% endfor %}
{% include paginator.html %}
Maybe, if your posts and tweets do have the same structure in terms of fields you could concat the two:
{% comment %} Given that you make the tweets accessible from site, as a collection, for example {% endcomment %}
{% assign posts = paginator.posts | concat: site.tweets | sort: "date" %}
{% for post in posts %}
<h2>{{ post.title }}</h2>
<div>{{ post.content }}</div>
{% endfor %}
Mind that, here I am also resorting the list after the concat, via the sort filter, so the tweets do appear in the normal time series of the posts.
And if your structures are not the same, you can always resort to the collection in which your post is:
{% assign posts = paginator.posts | concat: site.tweets | concat: site.instagram | sort: "date" %}
{% for post in posts %}
{% if post.collection == 'posts' %}
<h2>{{ post.title }}</h2>
<div>{{ post.content }}</div>
{% elsif post.collection == 'tweets' %}
{% comment %} If I am not mistaken, twitter have no title concept {% endcomment %}
<h2>There was a bird singing about:</h2>
<div>{{ post.content }}</div>
{% elsif post.collection == 'instagram' %}
{% comment %} So, now, it can fit any social media of your choice {% endcomment %}
{% endif %}
{% endfor %}
I'm trying to compare the current date with the date of the post in jekyll/liquid. If the current date is less than the date of the post, then I want to show the post title.
Here is my code so far:
<header class="announce-ticker">
<div class="container">
{% capture currentDate %}
{{ 'now' | date: '%s'}}
{% endcapture %}
{% assign eventCount = 0 %}
{% assign eventPosts = site.posts %}
{% for post in eventPosts %}
{% capture postDate %}
{{ post.date | date: '%s'}}
{% endcapture %}
{% if currentDate < postDate %}
{% post.title %}
{% assign eventCount = 1 %}
{% endif %}
{{ currentDate}}
{{ postDate }}
{% endfor %}
{% if eventCount == 0 %}
<p>No events</p>
{% endif %}
</div>
</header>
My problem is that it's not showing the post when it is greater than current date.
Any help would be appreciated. Thanks!
Future dated posts are not published by default. Basic way to enable them through command line is to use the future option:
jekyll serve --future
Alternatively, you can add the future parameter to your _config.yml:
future: true
More options in this article.
I am trying to create an archive page of posts from my website. What I would like is to be able to have a pages for each list of posts by month in this format:
www.mywebsite.com/2016/11 would display all posts for November 2016.
Can I have a page for each month I have posted that is dynamically created each time I post in a new month? I don't want to have to manually create a new page for each month.
I already can group posts by year like so:
<ul>
{% for post in site.posts %}
{% assign currentdate = post.date | date: "%Y" %}
{% if currentdate != date %}
<li id="y{{currentdate}}">{{ currentdate }}</li>
{% assign date = currentdate %}
{% endif %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
Thanks for any help.
You can modify your date filter to integrate the month, e.g. date: "%B %Y". That's what I used in a layout, with a separate <ul> for each month.
From documentation, month-only values for the date filter are:
%b: abbreviated month name.
%B: full month name.
%m: month of the year (01 - 12).
Complete loop:
<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>{{ post.title }}</li>
{% endfor %}
</ul>
About generating the pages, AFAIK this can only be done through a plugin. If you can't use plugins, e.g. if you're hosting your pages on GitHub, the best you can do is to reduce your pages to a short YAML frontmatter relying on a layout, like in this answer.
This is my markup for the list view of posts:
<ul class="eventlist">
{% for post in site.posts reversed %}
<li class="eventlist-element">
{% if post.href %}
<a class="eventlist-element__link" href="{{ post.href }}">
{% else %}
<a class="eventlist-element__link" href="{{ post.url | prepend: site.baseurl }}">
{% endif %}
<time datetime="{{ post.date | date: "%Y-%m-%d" }}"class="eventlist-element__date">
<span class="eventlist-element__date-day">{{ post.date | date: "%d" }}</span>
<span class="eventlist-element__date-day-name">{{ post.date | date: "%a" }}</span>
</time>
<div class="eventlist-elemnt-infowrap">
<span class="eventlist-element__title">{{ post.title }}</span>
<span class="eventlist-element__venue">{{ post.venue }}</span><span class="eventlist-element__time">{{ post.time }}</span><span class="eventlist-element__ticket">{{ post.ticket }}</span>
</div>
<span class="eventlist-element__bullets"></span>
</a>
</li>
{% endfor %}
</ul>
I want to iterate over all posts and want to compare the post.date. If we have let's say 3 posts on the same day I want markup a for post 1 and for post 2 and 3 markup b. I've tried a few things but nothing worked so far.
Try something along these lines:
{% assign previousDate = site.date %}
{% for post in site.posts %}
{% if post.date == previousDate %}
// some other class
{% endif %}
{% assign previousDate = post.date %}
{% endfor %}
Basically you assign the date of the previous post to some variable and compare it with the current posts date. If they are the same you apply a different style. Not sure if you can use date as is, does it also contain hours, minutes,..? If so you will want to filter it to get out only the year, month, day.
Some code :
{% assign defaultStyle = 'defaultStyle' %}
{% assign alternateStyle = 'alternateStyle' %}
{% assign counter = 1 %}
{% for post in site.posts %}
{% if previousPostDate %}
{% if previousPostDate == post.date %}
{% capture counter %}{{ counter | plus: 1 }}{% endcapture %}
{% else %}
{% assign counter = 1 %}
{% endif %}
{% endif %}
{% if counter | to_number > 1 %}
{% assign defaultStyle = alternateStyle %}
{% endif %}
<div class="{{ defaultStyle }}">
<h3>{{ post.title }}</h3>
<p>{{ post.date }}</p>
</div>
{% assign previousPostDate = post.date %}
{% endfor %}
The code implements :
If I'm not the first post in a date group my style is different.
Your logic was
If I'm not the first post in a date group of three posts (or more) my style is different.
The part date group of three posts or more implies that you already know how many "same date posts" you have and makes the code more complex.