获取数组中的 next/previous 项目,仅当它与今天的日期匹配时才按自定义 frontmatter 排序
Get the next/previous item in the array sorted by custom frontmatter only if it matches the todays date
更新:
我有一个名为事件的集合。此集合中的每个项目都有一个自定义元 "Start time and date:",它作为日期时间格式输入(例如 2017-02-23 00:05:09 +0700)。在我的主页布局中,我只列出 "Start time and date:" 匹配今天日期的事件。
{% assign events = site.events | sort: 'Start time and date' %}
{% for event in events %}
{% assign today = site.time | date: "%b %d" %}
{% assign eventdate = event['Start time and date'] | date: "%b %d" %}
{% if eventdate == today %}
...
{% else %}
{% endif %}
{% endfor %}
这会生成与今天日期匹配的事件列表,但系统中的事件顺序(不在主屏幕上呈现的列表中)按默认排序 page.date 而不是事件['Start time and date'].当我位于事件页面中并调用 page.next 时,我得到的不是我生成的今天事件列表中的下一个事件,而是 page.next,它是下一个事件页面,以便所有事件的创建(我假设它们是按日期排序的)。
如何使用自定义元事件 ['Start time and date'] 而不是默认 event.date 来访问与当前事件相关的下一个和上一个事件。基本上得到今天日期的网络或以前的事件。
请帮忙,我已经用尽了我的 jekyll 知识。
所以经过长时间的努力,我能够以这种方式解决,可能有一种更简单、更干净的方法来做到这一点,所以如果有人可以对此进行编辑以整理它,我将不胜感激 =)
{% assign today = site.time | date: "%d" %} // find current date
{% assign allevents = site.events | sort: 'Start time and date' %} // sort all items in collections event by custom metafield similar to the standard date field
{% for event in allevents %} // loop through all events
{% assign eventdate = event['Start time and date'] | date: "%d" %} // assign event date from the custom meta in the event item
{% if eventdate == today %} // check if event date equals today
{% if event.url == page.url %} // check if event url is also currently generated page url
{% assign preveventindex = forloop.index | plus: 1 %} // find the index of previous event in the array
{% assign nexteventindex = forloop.index | minus: 1 %} // find the index of next event in the array
{% for event in allevents %} // loop throug events again
{% assign today = site.time | date: "%d" %} // set current date again
{% assign eventdate = event['Start time and date'] | date: "%d" %} // set event date again
{% if eventdate == today %} // check if event generated has current date
{% if forloop.index == nexteventindex %} // check if next event index is correct
{{ forloop.index }} -
{{ event.url }}
{% assign nexteventurl = event.url %} // get the url is next item in the array has the same date as current item
// You can now use the url to navigate to the next item in the array that must have todays date
{% endif %}
{% if forloop.index == preveventindex %}
{{ forloop.index }} -
{{ event.url }}
{% assign preveventurl = event.url %}
// You can now use the url to navigate to the previous item in the array that must have todays date
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
{% endfor %}
更新: 我有一个名为事件的集合。此集合中的每个项目都有一个自定义元 "Start time and date:",它作为日期时间格式输入(例如 2017-02-23 00:05:09 +0700)。在我的主页布局中,我只列出 "Start time and date:" 匹配今天日期的事件。
{% assign events = site.events | sort: 'Start time and date' %}
{% for event in events %}
{% assign today = site.time | date: "%b %d" %}
{% assign eventdate = event['Start time and date'] | date: "%b %d" %}
{% if eventdate == today %}
...
{% else %}
{% endif %}
{% endfor %}
这会生成与今天日期匹配的事件列表,但系统中的事件顺序(不在主屏幕上呈现的列表中)按默认排序 page.date 而不是事件['Start time and date'].当我位于事件页面中并调用 page.next 时,我得到的不是我生成的今天事件列表中的下一个事件,而是 page.next,它是下一个事件页面,以便所有事件的创建(我假设它们是按日期排序的)。
如何使用自定义元事件 ['Start time and date'] 而不是默认 event.date 来访问与当前事件相关的下一个和上一个事件。基本上得到今天日期的网络或以前的事件。
请帮忙,我已经用尽了我的 jekyll 知识。
所以经过长时间的努力,我能够以这种方式解决,可能有一种更简单、更干净的方法来做到这一点,所以如果有人可以对此进行编辑以整理它,我将不胜感激 =)
{% assign today = site.time | date: "%d" %} // find current date
{% assign allevents = site.events | sort: 'Start time and date' %} // sort all items in collections event by custom metafield similar to the standard date field
{% for event in allevents %} // loop through all events
{% assign eventdate = event['Start time and date'] | date: "%d" %} // assign event date from the custom meta in the event item
{% if eventdate == today %} // check if event date equals today
{% if event.url == page.url %} // check if event url is also currently generated page url
{% assign preveventindex = forloop.index | plus: 1 %} // find the index of previous event in the array
{% assign nexteventindex = forloop.index | minus: 1 %} // find the index of next event in the array
{% for event in allevents %} // loop throug events again
{% assign today = site.time | date: "%d" %} // set current date again
{% assign eventdate = event['Start time and date'] | date: "%d" %} // set event date again
{% if eventdate == today %} // check if event generated has current date
{% if forloop.index == nexteventindex %} // check if next event index is correct
{{ forloop.index }} -
{{ event.url }}
{% assign nexteventurl = event.url %} // get the url is next item in the array has the same date as current item
// You can now use the url to navigate to the next item in the array that must have todays date
{% endif %}
{% if forloop.index == preveventindex %}
{{ forloop.index }} -
{{ event.url }}
{% assign preveventurl = event.url %}
// You can now use the url to navigate to the previous item in the array that must have todays date
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
{% endfor %}