We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7516
    • 5 Posts
    I've hit a bit of a brick wall here, so would appreciate some insight (if someone could oblige).

    I've set this up so that the main blog page displays an initial 3 posts when loaded fully.

    On clicking a "more posts" button underneath the 3 posts that are displayed, infinite scroll pagination (which has been set-up) kicks in but...I only want 2 posts (or 4, 5 etc.) at a time to be served not 3 thereafter.

    Can anyone help, it's probably obvious smiley

    Hope this makes sense.

    <?php
    $method   = $modx->getOption('method', $scriptProperties, 'initial');
    $limit    = $modx->getOption('limit', $scriptProperties, 3);
    $parent   = $modx->getOption('parent', $scriptProperties);
    $tpl      = $modx->getOption('tpl', $scriptProperties);
    $tplFirst = $modx->getOption('tplFirst', $scriptProperties);
    
    $load   = (int) $_GET['page'];
    $offset = 0;
    
    if($method == "initial") {
        if($load != 0) {
            $limit = $limit * $load;
        }
    } elseif($method == "pagination") {
        $offset = $limit * ($load - 1);
    }
    
    $page = $modx->runSnippet('getResources', array(
        'parents'        => $parent,
        'limit'          => $limit,
        'offset'         => $offset,
        'includeTVs'     => '1',
        'tvPrefix'       => '',
        'includeContent' => '1',
        'tpl'            => $tpl,
        'tplFirst'       => $tplFirst
    ));
    
    if($method == "initial") {
        return $page;
    } elseif($method == "pagination") {
        if(empty($page)) {
            $noResults = $modx->parseChunk('pagination-no-results',array('text' => 'No more posts!'));
            return json_encode(array(
                'success' => false,
                'page'    => $noResults
            ));
        } else {
            return json_encode(array(
                'success' => true,
                'page'    => $page
            ));
        }
    }
      "I design web stuff.
      I’m a dad, a gamer
      & a vinyl junkie."
      • 3749
      • 24,544 Posts
      How about putting a plain old getResources tag on the page to display the first three, then having your snippet respond to the "see more posts" submission?

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 7516
        • 5 Posts
        Quote from: BobRay at Jul 11, 2015, 03:22 PM
        How about putting a plain old getResources tag on the page to display the first three, then having your snippet respond to the "see more posts" submission?


        Thanks BobRay, taking a step back has helped me get to the point where it's almost there. Only one outstanding issue now though.

        Changed limit to:
        $limit    = $modx->getOption('limit', $scriptProperties, 2);


        The initial 3 posts displayed are sorted by date order on the main landing page (most recent first) e.g. Article 1, Article 2 & Article 3 (as per BobRay suggestion).

        But when clicking on More Posts - Article 3 (again) & Article 4 are returned - instead of Article 4 & Article 5.

        Any help is appreciated in solving this.

        NB Should add I'm still using exciting code (above). [ed. note: discofonzy last edited this post 11 years, 3 months ago.]
          "I design web stuff.
          I’m a dad, a gamer
          & a vinyl junkie."
          • 3749
          • 24,544 Posts
          Maybe you could make "See more Posts" a submit button in a tiny form. That way your snippet could know whether it was clicked by checking the $_POST array.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 7516
            • 5 Posts
            Thanks BobRay, I'll look into it smiley
              "I design web stuff.
              I’m a dad, a gamer
              & a vinyl junkie."