We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38723
    • 165 Posts
    So I've used bits and pieces from all over the place to try and get this working. Here's what I have:

    <?php
    $c = $modx->newQuery('modResource');
    $c->where(array(
        'published' => 1,
        'template' => 10,
    
    ));
     
    $total = $modx->getCount('modResource',$c);
    $totalVar = $modx->getOption('totalVar', $scriptProperties, 'total');
    $modx->setPlaceholder($totalVar,$total);
    
    $tpl = $modx->getOption('tpl', $scriptProperties, '');
    $limit = $modx->getOption('limit',$scriptProperties,6);
    $offset = $modx->getOption('offset',$scriptProperties,5);
    $c->sortby('publishedon','DESC');
    $c->limit($limit,$offset);
    
    $collection = $modx->getCollection('modResource',$c);
    
    foreach($collection as $item) {
        $vars = $item->toArray();
        $out[] = $modx->getChunk($tpl, $vars); 
    }
    
    return implode("\n", $out);
    


    If I call in a template like this:

    [[!pagingTest? &limit=`8` &offset=`5` &tpl=`newsLandingOther`]]
    


    I get exactly what I expect, minus the pagination. There are 7 items, and 2 appear here when I call it.

    So next, I try to wrap getPage around it like this:

    [[!getPage?
      &elementClass=`modSnippet`
      &element=`pagingTest`
      &tpl=`newsLandingOther`
      &pageVarKey=`page`
      &totalVar=`total`
      &limit=`4`
      &offset=`5`
    ]]
    


    Which, as previous issues with getPage I've run into ignores the offset I've set, even though the snippet definitely has the correct call. I must be doing something fundamentally stupid here because this: http://www.markhamstra.com/modx-blog/2011/12/preparing-custom-snippets-for-getpage/ and http://rtfm.modx.com/display/ADDON/getPage#getPage-OtherProperties suggest this should work.

    Kind of a follow on from this topic: https://forums.modx.com/thread/78109/articles-paging-and-getpage-are-ignoring-offset-in-parameters-list?page=2#dis-post-431004
      • 4172
      • 5,888 Posts
      thats untested, but I think you will need something like that with a additional property
      &addToOffset

      <?php
      
      $tpl = $modx->getOption('tpl', $scriptProperties, '');
      $limit = $modx->getOption('limit',$scriptProperties,6);
      $offset = $modx->getOption('offset',$scriptProperties,0);
      $addToOffset = $modx->getOption('addToOffset',$scriptProperties,0);
      $offset = $offset + $addToOffset;
      
      $c = $modx->newQuery('modResource');
      $c->where(array(
          'published' => 1,
          'template' => 10,
       
      ));
        
      $total = $modx->getCount('modResource',$c);
      $total = $total - $addToOffset;
      $total = $total < 0 ? 0 : $total;
      
      $totalVar = $modx->getOption('totalVar', $scriptProperties, 'total');
      $modx->setPlaceholder($totalVar,$total);
      
      $c->sortby('publishedon','DESC');
      $c->limit($limit,$offset);
       
      $collection = $modx->getCollection('modResource',$c);
       
      foreach($collection as $item) {
          $vars = $item->toArray();
          $out[] = $modx->getChunk($tpl, $vars);
      }
       
      return implode("\n", $out);
      


      which you then, if it works, can call like that:

      [[!getPage?
        &elementClass=`modSnippet`
        &element=`pagingTest`
        &tpl=`newsLandingOther`
        &pageVarKey=`page`
        &totalVar=`total`
        &limit=`4`
        &addToOffset=`5`
      ]]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 22303 MODX Staff
        • 10,725 Posts
        The offset is set by getPage based on the total and limit you pass it. These are passed to your Snippet and should not be modified or you are defeating the purpose of the paging.
          • 38723
          • 165 Posts
          Thanks for the reply. I figured about the offset and decided on a different approach in the end!