I am trying to make my entry loop have a load more button, and not sure how to do it. I tried the method from http://craftsnippets.com/articles/infinite-scrolling-and-lazy-loading-with-craft-cms but I could not get it to work.
Below is the code for my entry loop and "Load More" button. What would be the easiest way to make the "Load More" button functional?
<div class="more-posts-wrap">
{% set moreABetterPost = craft.entries.section('aBetterPost').all() %}
{% for entry in moreABetterPost %}
<a href="{{ entry.getUrl }}" class="more-post">
<div class="more-post-img-wrap">
{% set featuredImage = entry.featuredImage.one() %}
<img src="{{ featuredImage.getUrl }}" class="more-post-img">
<div class="red-overlay"></div>
</div>
<div class="more-post-content">
<div class="more-post-title">
<h3>{{ entry.title }}</h3>
</div>
<div class="next-icon">
<i class="fas fa-chevron-right"></i>
</div>
</div>
</a>
{% endfor %}
</div>
<div class="load-button-wrap">
<a href="#">
<div class="load-button">
<p>Load More</p>
</div>
</a>
</div>
EDIT: Clarifying the structure of my site.
{# _main.twig #}
Header Stuff
Header Stuff
...
{% block content %}
{# All of my pages exist in here by extending _main.twig #}
Page Stuff
Page Stuff
Page Stuff
...
LOAD MORE ENTRIES AREA
{% endblock %}
Footer Stuff
Footer Stuff
Footer Stuff
...
{# End of _main.twig #}
EDIT 2: Showing the code that's not working in regards to James Smith's Answer.
_loadMore.twig
{% set theLayout = craft.app.request.isAjax and not craft.app.request.isLivePreview ? '_ajaxLayout' : '_main' %}
{% extends theLayout %}
<section>
<div class="more-posts-section">
<div class="section-header">
<h2>More Posts in <span>{{ category.title }}</span></h2>
</div>
{% set entries = craft.entries.section('aBetterPost').limit(6) %}
{% paginate entries as pageInfo, pageEntries %}
{% block content %}
<div class="more-posts-wrap-border">
<div class="more-posts-wrap js-ajaxPagination">
{% for entry in aBetterPost%}
{% include '_layouts/_post' %}
{% endfor %}
</div>
</div>
{% endblock %}
<div class="load-button-wrap">
<a href="{{ '/' ~ craft.app.request.pathInfo ~ '/p' ~ (pageInfo.currentPage + 1) }}" class="load-button js-loadMore">{{'load more'|t}}</a>
</div>
{% block ajaxContent %}
{% for entry in aBetterPost%}
{% include '_layouts/_post' %}
{% endfor %}
{% endblock %}
</div>
</section>
{# ======================================
AJAX PAGINATION SCRIPT
========================================= #}
{% if pageInfo is defined and pageInfo.total > 6 %}
{% js %}
var currentPage = 1;
var perPage = 6;
var count = perPage;
$('.js-loadMore').click(function(e){
e.preventDefault();
if({{ pageInfo.total }} > count){
$.ajax({
url: $(this).attr('href'),
}).done(function(data) {
var $new = $('.js-ajaxPagination').append(data);
var $button = $('.js-loadMore');
// increment the counters
count = count + perPage;
currentPage++;
$button.show().attr('href', '/{{ craft.app.request.pathInfo }}/p' + (currentPage + 1));
if({{ pageInfo.total }} <= count){
$button.remove();
}
});
}
});
{% endjs %}
{% endif %}
_page.twig
{% extends "_layouts/_main" %}
{% block content %}
{# Other Page Stuff#}
{# Other Page Stuff#}
{% include '_layouts/_loadMore' %}
{% endblock %}