4

I should begin to say that I have no idea really how exactly ajax works. Although I've used it a couple of times with pre-made WordPress plugins. I saw some examples of ajax entry-loading here on Stack Exchange, I tried to make ajax requests in Craft but just ended up with server errors.

My main goal that I want to accomplish is just to have a news-feed that begin to show 5 entries, a button on the bottom that appends five more.

  • Is it best to use jQuery for the Ajax-request?
  • How can I load the "next" five entries in a news-feed, so the call doesn't load the latest five entries over and over?
  • I've seen that you need to build a specific template for ajax-calls. Is there any good examples of those?

If someone got some advice for me how to accomplish this it would make me so happy! It'd be really nice to learn how it works, so if you got tips of tutorials, working examples, good reads — please share :)

6

Is it best to use jQuery for the Ajax-request?`

Not necessarily. jQuery provides an easy-to-use ajax function, but nowadays it's perfectly simple to make an AJAX call with vanilla JS.

How can I load the "next" five entries in a news-feed, so the call doesn't load the latest five entries over and over?

You will need to pass an offset and limit to your element query, or use Craft's paginate tag, in order to only receive data for one 'page' of elements at a time.

I've seen that you need to build a specific template for ajax-calls. Is there any good examples of those?

I prefer to use the Element API plugin to serve up my JSON for front-end consumption... but you can also build a JSON feed using a Twig template.


Here are some links that might be helpful:

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.