Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Collect dates from blog posts

Started by Matthias 9 years ago · 3 replies · 631 views
9 years ago

Is there a way to collect all dates from my blog posts? The reason I am asking is that I want to activate only the dates i a datepicker, where blogposts exist.

9 years ago

I'm guessing you're using javascript for this right?

In twig, you could loop through all of the blog posts and just output their date.

Something like this.

TWIG
{% set collection = page.collection({ 'items': { '@self.descendants' }) %} // If you are on the page that holds all the blog posts
{% for post in collection %}
    <div class='hidden blog-date'>{{post.date}}</div> // Whatever date value you're using
{% endfor %}

Or you could install this Grav Pages as Data plugin, and get the data using AJAX, or whatever you use for HTTP Requests.

9 years ago

That looks good, thank you.
Another question: I want to pass these dates to the jquery datepicker. I have to do this as array. Can i collect these dates in an array instead of writing them out?

9 years ago

Something like this should work if you're using jQuery. It should get you on the right track at least.

JS
var dates = [];
$('.blog-date').each(function(dateElement) {
    dates.push(Date.parse(dateElement.text());
}

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 74 6 hours ago
General · by pamtbaau, 11 hours ago
1 47 10 hours ago
General · by Andy Miller, 23 hours ago
0 43 23 hours ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 39 5 days ago