‘Blog archives’ tutorial in Kirby Docs is not ready yet and tutorial on filtering by dates is beyond my skills Forum search proves that most people here deal with filtering as smth obvious, so may be forum ninjas can help me with my lamer question…
I want to have smth like this in my navbar in order to get lists of articles sorted by months:
CSS:
nav ul ul {display: none;}
.years:focus ~ .sub-menu,
.years:active ~ .sub-menu,
.years:hover { display: inline; }
Possibly if I try hard enough I manage to edit each month link manually, using docs instructions on filtering by date, but the question is: how to make this work automatically, to make appear only those months, that contain articles (yeah, my blogging style is very reluctant). Another question is if the result of these filtering will appear on ‘defalt’ template or should I make special arrangements for that? At the moment my search results are using special template and tag filtering results apper in the main blog template.
If you click on one of those links, the URL will contain the year and month.
In your controller or template, you can then get those values using param('year') and param('month'):
Thank you, Lukas, I appreciate your answer very much. Everything looks quite clear to me. But I won’t use this method, as I’m not using the panel and publish directly through Dropbox, so simple plain folder structure outweighs archives feature in my mind. @bastianallgeier I think this answer can be used for Blog Solution Docs section
I think with Kirby 2.3 you can use the $pages->group() method with a callback (a function that will be executed for each page and which should return a value you want to group with).
<?php
function pageYear($p) {
return $p->date('Y'); // year, e.g. "2016"
}
function pageMonth($p) {
return $p->date('F'); // month name for the current locale, e.g. "January"
}
$posts = page('blog')->children();
?>
<?php foreach ($posts->group('pageYear') as $year => $yearList): ?>
<h2><?php echo $year ?></h2>
<?php foreach ($yearList->group('pageMonth') as $month => $monthList): ?>
<h3><?php echo $month ?><h3>
<ul>
<?php foreach ($monthList as $post): ?>
<li><a href="<?php echo $post->url() ?>"><?php echo $post->title() ?></a></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
<?php endforeach; ?>
Haven’t actually run this code but it should be working
It’s not really simple to write or even to read if you don’t have much programming experience, but unless Kirby adds the logic above as a feature it can’t be made much simpler.
Great thanx, Fvsch!
That is not exactly what I wanted but seems it’s the best what can be done without site structure tweaking.
I styled it my way and it goes to work
The result of @fvsch solution is multilevel list of links, located in one place (navbar in my case).
I wanted ‘months’ links lead to a new page containing that month articles, styled as main blog page - the same way as my search results, or articles filtered by tags are shown.
You mix $posts and $articles, depending what you pass to your template, you should make a decision, which variable you want to use for your articles
BTW: When posting blocks of code you can improve readability by wrapping code blocks within three backticks at the beginning and the end of a block on a separate line. I have corrected your code above. To see how it works, click on the pencil icon of your post. Thank you.
Thank you, texnixe. For some reason this code produces regular unsorted list of articles when opening links like siteurl.com/year:2016/month:April
At the same time tag filtering works fine.
At 3 A.M.?! Sonja, it works! You make me finally add that small snippet ‘made with kirby & love’ in my footer. Tnank you. Blog archive issue is solved.
It behaves similarily to the code discussed above except for the feature with the “active” class, added to the opened link. How to add this functionality to the month filtering code?