Hi,
I’m trying to merge two collections into a new one by adding one item of $B to every nth item of $A:
$A = $programItems->
filter(function($child) {
$child->date("Y-m-d", 'start_date') < date("Y-m-d") &&
$child->date(null, 'start_date') > strtotime('-6 month') &&
!($child->hasChildren()) ;
});
$B = $pages->
find('programs')->
children()->
find('events','projects')->
index()->
visible()->
return
!($child->hasChildren()); })->
shuffle()->
limit(10);
I’ve trawled through the forum and have seen:
Hi !
I’m building a website where, on the homepage, news and projects are mixed but organised by date, then for each category there is an attributed page ‘projects’ and ‘news’ which I achieved.
However for the homepage, I don’t really know how could I say : for each ‘project’ and ‘news’…
For the project I’ve written the variable like this :
<?php
$projects = page('projects')->children()->visible()->sortBy('date', 'desc');
?>
then I called it like this :
<?php foreach($projects as $project)…
Hello fellow Kirby lovers.
I’m having some trouble with pagination.
So, there is two collection objects, both contains somes pages of the website.
The first collection (let’s name it $one) contains some articles.
The second collection (so $two) contains a lot of blog posts or equiv.
Both are displayed on the home page, like a ‘two speeds carrousel’. But with sources from different parts of a webiste.
The aim is to show only one item of $one and 28 items of $two on each page…
$pages->add($collection);
But can’t figure out an elegant way to do the above without resulting to some kind of loop.
thanks!
Scratched my own itch and created a pages method:
/**************************************************
Interleave
Returns a new collections with item A inserted for every nth item B
**************************************************/
pages::$methods['interleave'] = function($pages, $items, $chunkNum=2) {
$newItems = new Pages();
$pagesChunks = $pages->chunk($chunkNum);
$i=0;
foreach ($pagesChunks as $chunkItems) {
$newItems->add($items->nth($i));
$newItems->add($chunkItems);
$i++;
}
return $newItems;
};
Usage:
$C = $A->interleave($B,4);