I make posts in two folders, call them /a/ and /b/.
Is it possible on the index page to display all the posts from both of those folders, sorted by date?
I make posts in two folders, call them /a/ and /b/.
Is it possible on the index page to display all the posts from both of those folders, sorted by date?
If all posts have the same template you could use this:
$posts = $site->index()->filterBy('template', 'YOURTEMPLATE')
Otherwise you could merge two collections like this:
<?php
$posts_a = $site->find('a')->children();
$posts_b = $site->find('b')->children();
$posts = new Pages(array($posts_a, $posts_b));
?>
Found here: Merge Collection of Pages
I’d go for the second option @flokosiol suggested, and to sort by date:
$posts = $posts->sortBy('date', 'desc'); //or asc, depending on what you want to achieve.
Oh yes, of course. I missed that …
Hey thanks @flokosiol and @texnixe, this looks perfect.
I’m a little confused on the actual implementation of the code, do I somehow include it with a foreach
statement like how I would on the individual pages?
You loop through the posts collection after merging:
<?php
$posts_a = $site->find('a')->children();
$posts_b = $site->find('b')->children();
$posts = new Pages(array($posts_a, $posts_b))->sortBy('date', 'desc'); ?>
foreach($posts as $post) : ?>
//do stuff
<?php endforeach ?>
I can’t seem to get the first part to work, the page just goes blank. In my case the two folders I put my posts in are called “buy” and “bought” ie (/content/buy/11223333-post/post.txt).
<?php snippet('header') ?>
<ul class="grid">
<?php
$posts_a = $site->find('bought')->children();
$posts_b = $site->find('buy')->children();
$posts = new Pages(array($posts_a, $posts_b))->sortBy('date', 'desc'); ?>
<?php foreach($posts as $post) : ?>
<li>
post stuff...
</li>
<?php endforeach ?>
</ul>
<?php snippet('footer') ?>
Have you turned on debugging in your config.php? What is the error?
One idea is that you can’t just use the string syntax “sortBy” like above, try putting it on a different line or put the new Pages thingy in parenthesis before adding sortBy().
$posts = new Pages(array($posts_a, $posts_b));
$posts = $posts->sortBy('date', 'desc');
Aha that was it! They had to be on separate lines.
Thanks again for the help, I’ve been struggling the last couple days to find out how to merge collections in these flat-files CMSes, StaceyCMS had me pulling my hair out yesterday but Kirby looks perfect!
Yeah, Kirby is great, and just on a side note, Stacey does not seem to be under active development anymore. The last commit on Github was 2 years ago and the same is true for quite a few other flat-file CMS.
Oh for sure I have Kirby running for two other projects, love it.
But this is a super-super-side project so I was looking to avoid the 15€ and because I’ve used Stacey before, but to be honest, it’s totally worth it every time.
// EDIT
Nevermind. This was just me forgetting about looking out for captial letters in folder names…
Been too long since I worked with Kirby. Now it works perfectly!