1

Following up on a previous question of mine: How to skip the first few entries in a "load more" scenario with Element API?

I have a website that utilizes the "load More" (button) method shown here: http://craftsnippets.com/articles/infinite-scrolling-and-lazy-loading-with-craft-cms

On my site, I have a homepage that displays the first 6 post in a featured way, then below I have a "more posts" area. In my previous question, I was shown how to deal with skipping the fist 6 posts in the loop of the "load more" area.

However, I want to use the "load more" button elsewhere on the site. Such as:

  1. Category Page – Same as the Homepage, but only showing more entries in whatever category the page is.
  2. Individual Entry Page – In this instance I need the "Load More" area to not skip the first 6 entries.

I can't seem to figure out how use this method in different ways across the site, because the settings for one instance bleeds into the other instances. So, how can I accomplish this? (sorry if this is basic stuff. dealing with API is still new to me)

  • 1
    I still think you should just use the native pagination feature for this as I answered earlier (craftcms.stackexchange.com/a/35744/271) - it's much simpler than using the Element API and you can have different settings for each implementation. – James Smith Jul 9 at 6:54
2

You could add a new param to the request called skip and use that on your homepage, and default it to zero to allow you to reuse the endpoint elsewhere.

$skipEntries = Craft::$app->request->getParam('skip') ?? 0;
$criteria = [
    'section' => $settings['section'], 
    'limit' =>  $settings['limit'],
    'offset' =>  ($settings['limit'] * Craft::$app->request->getParam('offset')) + $skipEntries,
    'order' => $settings['orderBy'],
];

You'll need to add the skip parameter to the data object in the jQuery $.ajax request options object for your homepage implementation:

$.ajax({
    // some params here
    data: {
        skip: 6,
        offset: lazy_offset,
        settings: lazy_settings,
    },
    // some more params here
}

Other pages would omit the skip parameter. That's one way of solving the problem.

| improve this answer | |
  • That worked great! Any ideas on how to only grab entries of whatever category the page is? eg. load more entries of X category when you're on Page X? – Justin Kendall Jul 9 at 18:33
  • Currently when I add relatedTo(category) to the loop, it works sorta, but it will also load all the rest of the entries after it's gone through the entries of that category. – Justin Kendall Jul 9 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.