Load more images in matrix block I have a matrix block called team that has an assets field within it that has all of the companies headshots. However, there are more than 30 images, but the design is to only have 6 showing on load, and then on click of a load more button the next 6 will show, etc.

However, I'm not sure how to achieve this within a matrix block as all the examples I've seen elsewhere are for pages and sections. But I just need to load the images from within the matrix block.

Below is my code for the images. Can someone help?

{% for headshot in block.headshots %}
&lt;figure class="team__headshots"&gt;
  {% if headshot | length %}
  {% set transformedImages = craft.imager.transformImage(headshot,
      [
          { width: 180, ratio: 4/5 },
      ],
      {
          format: 'jpg',
          allowUpscale: false,
          mode: 'crop',
          jpegQuality: 80,
          position: headshot.focalPoint,
          interlace: true
      }
  ) %}
  {% set tabletl = craft.imager.transformImage(headshot,
      [
          { width: 142, ratio: 4/5 },
      ],
      {
          format: 'jpg',
          allowUpscale: false,
          mode: 'crop',
          jpegQuality: 80,
          position: headshot.focalPoint,
          interlace: true
      }
  ) %}
  {% set tabletp = craft.imager.transformImage(headshot,
      [
          { width: 230, ratio: 4/5 },
      ],
      {
          format: 'jpg',
          allowUpscale: false,
          mode: 'crop',
          jpegQuality: 80,
          position: headshot.focalPoint,
          interlace: true
      }
  ) %}
  {% set mobile = craft.imager.transformImage(headshot,
      [
          { width: 360, ratio: 4/5, jpegQuality: 65 },
      ],
      {
          format: 'jpg',
          allowUpscale: false,
          mode: 'crop',
          position: headshot.focalPoint,
          interlace: true
      }
  ) %}
  &lt;picture&gt;
  &lt;source srcset="{{ craft.imager.srcset(mobile) }}" media="(max-width: 767px)"&gt;
  &lt;source srcset="{{ craft.imager.srcset(tabletp) }}" media="(min-width: 768px) and (max-width: 991px)"&gt;
  &lt;source srcset="{{ craft.imager.srcset(tabletl) }}" media="(min-width: 992px) and (max-width: 1199px)"&gt;
  &lt;img class="img-responsive"
       src="{{ transformedImages[0].url }}"
       sizes="(min-width: 768px) 33.3vw, 50vw"
       alt="{% if headshot.altTag|length %}{{ headshot.altTag }}{% else %}{{ headshot.title }}{% endif %}"
       width="100%"&gt;
  &lt;/picture&gt;
  {% endif %}
  &lt;div class="team__headshots-overlay"&gt;
    &lt;div class="team__headshots-captions"&gt;
      &lt;h4&gt;{{ headshot.title }}&lt;/h4&gt;
      &lt;p&gt;{{ headshot.jobTitle }}&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/figure&gt;
{% endfor %}
Your block.headshots is not an array of all images, you have to understand what craft does in order to work properly with it.

block.headshots and all other relations (Matrix elements, images, category links...) are all elementCriteriaModels so instead of having an array of objects - your block.headshots is just a single object that has an iterator interface.

So when you execute {% for headshot in block.headshots %} it performs the .find() function and returns an array.
Unfortunately I'm not on my computer currently so I can't provide the exact code so maybe the syntax of something of the following might be wrong, but if you know how to search entries with craft.entries.find() click you can correct it, here you have more examples elementCriteriaModel. Your code should look like 

{% for headshot in block.headshots.find({limit: 10}) %}

It does not matter if you are searching pages (entries), assets, matrix blocks, sections, categories or whatever.. Craft does always the same, building an elementCriteriaModel that is executed as soon as you loop through it.

edit: your controller to load the assets could look like this

public function actionLoadAssets(){
    $this-&gt;requireAjaxRequest();
    $response = array(
        'success'   =&gt; false,
        'message'   =&gt; ''
    );

    // config how many entries should be loaded
    $config = craft()-&gt;globals-&gt;getSetByHandle('config');
    $limit = $config['countReloadImages'];
    // offset.. how many did we load already
    $offset = craft()-&gt;request-&gt;getPost('offset');
    // which block is it
    $matrixIndex = craft()-&gt;request-&gt;getPost('matrixIndex');
    // entryId
    $entryId = craft()-&gt;request-&gt;getPost('entryId');

    if($entry = craft()-&gt;elements-&gt;getElementById($entryId)){
        // search for the correct field -&gt; we just insert an offset
        // this is untested, maybe you have to play around with the correct number
        // I'm not sure if you can just send the id of the matrixfield and use 'id' =&gt; $matrixIndex
        // you could to try it out...
        $matrix = $entry-&gt;team-&gt;find([
            'offset'    =&gt; $matrixIndex
        ]);
        $html = '';
        if($matrix &amp;&amp; ($assets = $matrix-&gt;headshot-&gt;find(['offset' =&gt; $offset, 'limit' =&gt; $limit]))){
            // build a template to render the images, you'll return the html
            $html = $this-&gt;renderTemplate('some tempalte here', array(
                'asset' =&gt; $assets
            ));
        }
        // you could include your logic if you found less entries than configured ($limit) and return a "hide reload button"
        $response['html'] = $html;
        $response['success'] = true;
    }else{
        $response['message'] = Craft::t('could not find entry with Id ' . $entryId);
    }

    return $response;
}


Edit2: I found a better solution:

public function actionLoadMore(){
    $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Asset);
    // id of your field headtshot
    $field = craft()-&gt;fields-&gt;getFieldById(5);

    $criteria-&gt;relatedTo = array(
        'sourceElement' =&gt; //your matrix ID,
        'field'         =&gt; $field-&gt;id
    );
    $criteria-&gt;offset = //your offset;
    $criteria-&gt;limit = //your limit;
    $assets = $criteria-&gt;find();

    // render template -&gt; return html look at the first example
}