Good morning 
I’ve changed my plans for this. Instead of a grouped collection on a single page ($topics = page('views', 'editorial', 'publishing')->children()->listed()->groupBy('topic');
, there will now be a topics
page as well as a topic
template for subpages of the topics
page:
$items = page('items', 'editorial', 'publishing')->children()->listed()->filterBy('topic', $page->id());
this is working well when opening i.e mysite.com/topics/archive
, but I can’t really get the logic for the ajax load more json file to work. What I am trying to achieve, instead of having to create a json.php
for each of the topics’ page children, to have a central topic.json
that gets a topic
parameter via URL and renders only the needed topic, just like the regular page request would:
controllers/topic.php
if(r::ajax() && get('offset') && get('limit') && get('topic')) {
$offset = intval(get('offset'));
$limit = intval(get('limit'));
$topic = get('topic');
$items = page('items', 'editorial', 'publishing')->children()->listed()->filterBy('topic', $topic);
$more = $count > $offset + $limit;
} else {
$offset = 0;
$limit = 1;
$items = $items->limit($limit);
}
templates/topic.json.php
<?php
$html = '';
foreach($items as $item) {
$html .= snippet('topic_item', ['item' => $item], true);
}
$data['html'] = $html;
$data['more'] = $more;
echo json_encode($data);
script.js
var element = $('.posts_container');
var url = '../' + element.data('page') + '.json';
var limit = parseInt(element.data('limit'));
var offset = limit;
var topic = element.data('topic');
$('.load-more').on('click', function(e) {
$.get(url, {limit: limit, offset: offset, topic: topic}, function(data) {
if(data.more === false) {
$('.load-more').hide();
}
element.children().last().after(data.html);
offset += limit;
});
});
and:
<div class="grid-container-w12 full_page_bar_right posts_container"
data-page="topic"
data-limit="<?= $limit ?>"
data-topic="<?= $page->slug(); ?>"
>
Does that even make sense? where is the error in my thinking? Right now, trying to open the topic.json file redirects me to the default site. I did get it to work briefly but it was fetching the wrong items… I don’t remember what changes I made since then (what is git?) so I was hoping for some help in the forum :~~~)