Get the start and end date (year) of content folder with blog articles

Hi, I searched the documentation and the forum but with no luck.
In order to structure my blog, I divided it in small parts.
It is possible to get the date range in years for blog articles?

Blog A: 2011 – 2012
Blog B: 2015 – 2018
Blog C: 2019

My content folder:

content
    1-touren
       1-tour-a
           20150429-africa
       2-tour-b
       3-tour-c

Do you have a date field set correctly in each page?

Hi @jimbobrjames
yes
Date: 2015-04-29

You can filter between two dates, see here.

1 Like

Sorry, but I did not get it to work :frowning:

Can somebody please provide some code snippets, please?

Here is, what I have so far:
Controller:

<?php
	return function($site, $pages, $page) {
	$articles = $page->children()->visible();

	return compact('articles');
};

Template:

<?php foreach($articles as $article): ?>
	<a href="<?= $article->url() ?>" class="">
		<div class="">
			<?php snippet('image', array(
				'item' => $article,
				'thumb' => 'thumbnail'
			)) ?>
		</div>

		<div class="">
			<h2 class=""><?= $article->title()->html() ?></h2>
		</div>
	</a>
<?php endforeach ?>

The blog articles are actually grandChildren of this $page

Use grandchildren to fetch them grandChildren().

Your template doesnt seem to have anything to do with dates in it - are you trying to group the articles under year headings, or just simply list them out in date order?

Like I tried to describtet in my first post, I would like to print out the start- and end date of the blog posts:
Blog A: 2011 – 2012
Blog B: 2015 – 2018
Blog C: 2019

Sure but you don’t say what you actually want to happen. Just printing the date ranges out is one thing but not terribly useful…do you want to be able to click on the data ranges to see a list of those articles, or do you want to list the articles that fall between those dates under those headings on one page?

Here you go

Below the title I would like to print out the start and end date in years for the children.

I see…

You can probably just use first() and last() on your children and get the date field of that article, formatted to just years. Do this inside your article loop, because it uses the $article variable set on the loop.

<?php
$range = $article->children()->sortBy('date', 'asc');
?>
<?= $range->first()->date('Y');) ?> - <?= $range->last()->date('Y');) ?>

If you have named the date field something special, you need to pass that into date()…

->date('Y', 'YOURFIELDNAME')

Your code works quite well, thanks.
Unfortunately, it outputs the whole range for all children, not for the correspondending children:
cycoholic.org

<?php foreach($articles as $article): ?>
	<a href="<?= $article->url() ?>" class="">
		<div class="">
			<?php snippet('image', array(
				'item' => $article,
				'thumb' => 'thumbnail'
			)) ?>
		</div>

		<div class="">
			<h2 class=""><?= $article->title()->html() ?></h2>
			<?= $range->first()->date('%Y'); ?> - <?= $range->last()->date('%Y'); ?>
		</div>
	</a>
<?php endforeach ?>

Where and how did you set the $range variable… needs to go in the loop. If you put it in the controller and made it work off of $articles instead, then yes i think it will get the whole range.

Is not working:

<?php foreach($articles as $article): ?>
	<?php $range = $articles->children()->sortBy('date', 'asc'); ?>

	<a href="<?= $article->url() ?>" class="">
		<div class="">
			<?php snippet('image', array(
				'item' => $article,
				'thumb' => 'thumbnail'
			)) ?>
		</div>

		<div class="">
			<h2 class=""><?= $article->title()->html() ?></h2>
			<?= $range->first()->date('%Y'); ?> - <?= $range->last()->date('%Y'); ?>
		</div>
	</a>
<?php endforeach ?>

Yeah as my edit, your making it use $articles instead of $article. You want it to work off the children of each $article in the loop, so it should be:

<?php foreach($articles as $article): ?>
	<?php $range = $article->children()->sortBy('date', 'asc'); ?>

	<a href="<?= $article->url() ?>" class="">
		<div class="">
			<?php snippet('image', array(
				'item' => $article,
				'thumb' => 'thumbnail'
			)) ?>
		</div>

		<div class="">
			<h2 class=""><?= $article->title()->html() ?></h2>
			<?= $range->first()->date('%Y'); ?> - <?= $range->last()->date('%Y'); ?>
		</div>
	</a>
<?php endforeach ?>

On a side note, you shouldnt put block level html elements inside inline elements, but rather the other way round…

<?php foreach($articles as $article): ?>
	<?php $range = $article->children()->sortBy('date', 'asc'); ?>

		<div class="">
			<a href="<?= $article->url() ?>" class="">
			<?php snippet('image', array(
				'item' => $article,
				'thumb' => 'thumbnail'
			)) ?>
			</a>
		</div>

		<div class="">
			<h2 class=""><a href="<?= $article->url() ?>" class=""><?= $article->title()->html() ?></a></h2>
			<?= $range->first()->date('%Y'); ?> - <?= $range->last()->date('%Y'); ?>
		</div>

<?php endforeach ?>
1 Like

Awesome!
Thanks a lot @jimbobrjames

Is putting a div inside an anchor ever correct?

Yes, with HTML5 :slight_smile:

Don’t get me started… :slight_smile: . … i’m an old dog… building the internet since 1998. I know there are arguments for it with html 5 but it just feels wrong.

Glad you got it working.

1 Like

I really appreciate the effort from people here in the forum, especially from @jimbobrjames & @texnixe!

Thanks!

1 Like

An alternative way of achieving this:

<?php
$dates = page('blog')->children()->pluck('date', true);

$minYear = strftime('%Y',min($dates));
$maxYear = strftime('%Y',max($dates));
?>

Edit: Contrary to what I would have expected, this is, however, actually slower than the above suggestion

2 Likes