# Problem LoadMore Button

**URL:** <https://forum.getkirby.com/t/problem-loadmore-button/11183>\
**Category:** Questions\
**Tags:** v2\
**Created:** [September 9, 2018, 1:16pm UTC](https://forum.getkirby.com/t/problem-loadmore-button/11183 "2018-09-09T13:16:25Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![robzyye](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/robzyye/32/5668_2.png) [@robzyye](https://forum.getkirby.com/u/robzyye)\
**Post date:** [September 9, 2018, 1:16pm UTC](https://forum.getkirby.com/t/problem-loadmore-button/11183/1 "2018-09-09T13:16:25Z")

</div>

Hi, I would like to make a “Load More” button but I can’t make it work with my blog and to be honest I don’t really understand how it work, so there is “blog” (parent) and “blogarticle” (child).

_template/blog.php_\*

```auto
<div class"main">
	<div class="articles" data-page="<?= $page->url() ?>" data-limit="<?= $limit ?>">
	<!-- Loop through the projects -->
      <?php foreach($page as $article): ?>
      <?php snippet('homearticle', compact('homearticle')) ?>
      <?php endforeach ?>
	</div>
	<button class="load-more">Load more</button>
</div>

```

_template/blog.json.php_\*

```auto
<?php
$html = '';
foreach($page as $article) {
  // reuse the project snippet to create the HTML for each project
  // we need to set the third parameter to true, to return the
  // snippet content instead of echoing it
  $html .= snippet('homearticle', compact('homearticle'), true);
}
// add $html and $more to the $data array
$data['html'] = $html;
$data['more'] = $more;
// JSON encode the $data array
echo json_encode($data);
?>

```

_snippets/homearticle.php_\*

```auto
<div class="HomeArticle">
	<a href="<?= $article->url() ?>"><?= $article->title() ?></a>
	<!--always check if the image exists!-->
	<?php if($coverimage = $article->coverImage()->toFile()): ?>
		<a href="<?= $article->url() ?>"><img src="<?= $coverimage->url() ?>"></a>
	<?php endif ?>
</div>

```

_controllers/blog.php_\*

```auto
<?php
return function($site, $pages, $page) {
  $article = $page->children()->visible()->filterBy('template', '==', 'blogarticle')->unscheduled();
  $count = $article->count();
  // check if the request is an Ajax request and if the limit and offset keys are set
  if(r::ajax() && get('offset') && get('limit')) {
    // convert limit and offset values to integer
    $offset = intval(get('offset'));
    $limit = intval(get('limit'));
    // limit projects using offset and limit values
    $page = $article->offset($offset)->limit($limit);
    // check if there are more projects left
    $more = $count > $offset + $limit;
  // otherwise set the number of projects initially displayed
  } else {
    $offset = 0;
    $limit = 2;
    $projects = $article->limit($limit);
  }
  return compact('offset', 'limit', 'article', 'more');
};
?>

```

_assets/js/script.js_\*

```auto
$(function(){

  var element = $('.page');
  var url = element.data('page') + '.json';
  var limit = parseInt(element.data('limit'));
  var offset = limit;

  $('.load-more').on('click', function(e) {

    $.get(url, {limit: limit, offset: offset}, function(data) {

      if(data.more === false) {
        $('.load-more').hide();
      }

      element.children().last().after(data.html);

      offset += limit;

    });

  });

});

```

So if I understand, it will load the snippet that will add 2 more article in the div class=“articles”, but if we use limit instead of pagination, can we make the button load more disapear if there is no more article to load ? We can’t check if there are next page …

Thanks 🙃

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [September 9, 2018, 1:36pm UTC](https://forum.getkirby.com/t/problem-loadmore-button/11183/2 "2018-09-09T13:36:58Z")

</div>

Please follow the [cookbook recipe](https://getkirby.com/docs/cookbook/ajax-load-more), you have changed some variables/element names but are not using them consistently.

- In template, you loop through $page, but $page is the current page, not your articles collection.
- Then you pass a variable `homearticle` to the snippet that is nowhere defined.
- In your controller, you define `$article`(which should actually be $articles), but then you suddenly define $page and `$projects`, where you should stick to the `$article`($articles) variable.
- In your script, you look for an element with the class `page`, but that element doesn’t exist, it is called `articles`.

> [@robzyye](#):
>
> We can’t check if there are next page …

No, that is not necessary, you don’t need to check if there. are more pages, but you check if there are more articles.

---

<div class="post-metadata">

**Author:** ![robzyye](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/robzyye/32/5668_2.png) [@robzyye](https://forum.getkirby.com/u/robzyye)\
**Post date:** [September 9, 2018, 2:42pm UTC](https://forum.getkirby.com/t/problem-loadmore-button/11183/3 "2018-09-09T14:42:05Z")

</div>

I make changes but when I click on the button, there is nothing.

**_template/blog.php_**

```auto
<div class"main">
	<div class="articles" data-page="<?= $page->url() ?>" data-limit="<?= $limit ?>">
		<!-- Loop through the projects -->
		<?php foreach($articles as $article): ?>
			<?php snippet('article', compact('article')) ?>
		<?php endforeach ?>
	</div>
	<button class="load-more">Load more</button>
</div>

```

**_template/blog.json.php_**

```auto
<?php
$html = '';
foreach($articles as $article) {
  // reuse the project snippet to create the HTML for each project
  // we need to set the third parameter to true, to return the
  // snippet content instead of echoing it
  $html .= snippet('article', compact('article'), true);
}
// add $html and $more to the $data array
$data['html'] = $html;
$data['more'] = $more;
// JSON encode the $data array
echo json_encode($data);
?>

```

**_snippets/article.php_**

```auto
<div class="HomeArticle">
	<a href="<?= $article->url() ?>"><?= $article->title() ?></a>
	<!--always check if the image exists!-->
	<?php if($coverimage = $article->coverImage()->toFile()): ?>
		<a href="<?= $article->url() ?>"><img src="<?= $coverimage->url() ?>"></a>
	<?php endif ?>
</div>

```

**_controllers/blog.php_**

```auto
<?php
return function($site, $pages, $page) {
    $articles = $page->children()->visible()->filterBy('template', '==', 'blogarticle')->unscheduled();
    $count = $articles->count();
    // check if the request is an Ajax request and if the limit and offset keys are set
    if(r::ajax() && get('offset') && get('limit')) {
        // convert limit and offset values to integer
        $offset = intval(get('offset'));
        $limit = intval(get('limit'));
        // limit projects using offset and limit values
        $articles = $articles->offset($offset)->limit($limit);
        // check if there are more projects left
        $more = $count > $offset + $limit;
        // otherwise set the number of projects initially displayed
    } else {
        $offset = 0;
        $limit = 3;
        $articles = $articles->limit($limit);
    }
    return compact('offset', 'limit', 'articles', 'more');
};
?>

```

**_assets/js/script.js_**

```auto
$(function(){

  var element = $('.articles');
  var url = element.data('page') + '.json';
  var limit = parseInt(element.data('limit'));
  var offset = limit;

  $('.load-more').on('click', function(e) {

    $.get(url, {limit: limit, offset: offset}, function(data) {

      if(data.more === false) {
        $('.load-more').hide();
      }

      element.children().last().after(data.html);

      offset += limit;

    });

  });

});

```

What disturb me in the tutorial is that the page is called projects.php and project.php etc… my main page is blog.php and blogarticle.php

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [September 9, 2018, 2:58pm UTC](https://forum.getkirby.com/t/problem-loadmore-button/11183/4 "2018-09-09T14:58:19Z")

</div>

Are you loading jQuery? Put a `console.log('click');` as first thing after load more on click to check if it registers the click event.

It doesn’t matter what your files are called.

---

<div class="post-metadata">

**Author:** ![robzyye](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/robzyye/32/5668_2.png) [@robzyye](https://forum.getkirby.com/u/robzyye)\
**Post date:** [September 9, 2018, 3:03pm UTC](https://forum.getkirby.com/t/problem-loadmore-button/11183/5 "2018-09-09T15:03:50Z")

</div>

There is a error message few sec after I click on the button.

 ![error](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/e/ea9a5a8dd2ef5f7b3dc514d938f2f4091b2bd68c.jpeg)

(Thanks for you time)

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [September 9, 2018, 3:15pm UTC](https://forum.getkirby.com/t/problem-loadmore-button/11183/6 "2018-09-09T15:15:52Z")

</div>

The URL looks strange. Is you blog on the home page? Wondering why the blog part is missing…

If is is on the home page, you have to adapt the URL you call in the script slightly:

```auto
  var url = element.data('page') + '/.json';

```

---

<div class="post-metadata">

**Author:** ![robzyye](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/robzyye/32/5668_2.png) [@robzyye](https://forum.getkirby.com/u/robzyye)\
**Post date:** [September 9, 2018, 3:48pm UTC](https://forum.getkirby.com/t/problem-loadmore-button/11183/7 "2018-09-09T15:48:21Z")

</div>

You know your thing, there are no more error but still nothing loaded 🤔

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [September 9, 2018, 4:18pm UTC](https://forum.getkirby.com/t/problem-loadmore-button/11183/8 "2018-09-09T16:18:34Z")

</div>

Check what is in the request and what response you get in dev tools.

Make sure there are more articles that meet the condition of being visible and scheduled and with this template.

---

<div class="post-metadata">

**Author:** ![robzyye](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/robzyye/32/5668_2.png) [@robzyye](https://forum.getkirby.com/u/robzyye)\
**Post date:** [September 9, 2018, 8:35pm UTC](https://forum.getkirby.com/t/problem-loadmore-button/11183/9 "2018-09-09T20:35:47Z")

</div>

So I’ve try with only

```auto
$articles = $page->children()->visible();

```

And when I click that is the response, it looks like that limit and offset aren’t detected ?

 ![Capture](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/d/d4a99ee014f1012841a9522298d83105b64769d9.jpeg)

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [September 9, 2018, 9:05pm UTC](https://forum.getkirby.com/t/problem-loadmore-button/11183/10 "2018-09-09T21:05:16Z")

</div>

Hm 🤔; I think I have to see this in action. Could you provide a zip for download somewhere (send me a PM if you want).

---

<div class="post-metadata">

**Author:** ![robzyye](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/robzyye/32/5668_2.png) [@robzyye](https://forum.getkirby.com/u/robzyye)\
**Post date:** [September 10, 2018, 12:27pm UTC](https://forum.getkirby.com/t/problem-loadmore-button/11183/11 "2018-09-10T12:27:45Z")

</div>

The problems were the redirection in my error.php page and the route to avoid the /blog/ in the url, now it’s working 😉

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:26pm UTC](https://forum.getkirby.com/t/problem-loadmore-button/11183/12 "2025-02-06T17:26:11Z")

</div>

Archived on February 6, 2025, 5:26pm UTC


