We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6649
    • 25 Posts
    Hi guys, in spot of bother.

    Can someone please enlighten me to some coding:

    I would like to have a ’Next’ button on my template, so that when I create a new document, press the next button and it automatically goes to the next ’ID’ page.

    Please can someone give me some code to be able to do this, strangely enough I cant find anything about this anywhere so after hours of looking I am posting a topic.

    It is important for this to be automatic, sure you realise, that having a link to (eg) ID 3 in the template will be useless because it will loop on the same page. I need it to just go to the next ID page. Thanks very much everyone

    D smiley
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You can use the PrevJumpNext snippet and skip the Jump part.

      Or, use a snippet
      $newId = $modx->documentIdentifier +1;
      $link = $modx->makeUrl($newId);
      return $link;
      

        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 6649
        • 25 Posts
        Thanks very much for the help.

        I have got hold of the PJN snippet, however ... can you just give me an example? This is the code I am using and where I want to the link on the template, if you can demonstrate that would be great.

        Thanks again!!!

        <div class="button">
        <a href="**Link here**" target="_parent">Next page >></a>
        </div>

        D smiley
          • 4310
          • 2,310 Posts
          Susans example works perfectly.
          Create a snippet named "Next" with this code:
          <?php
          $newId = $modx->documentIdentifier +1;
          $link = $modx->makeUrl($newId);
          return $link;
          ?>

          In your template or documents add:
          <div class="button">
          <a href="[!Next!]" target="_parent">Next page >></a>
          </div>
            • 22303 MODX Staff
            • 10,725 Posts
            FYI, that $modx->documentIdentifier + 1 is not the way to go about this; as that would return the next integer value, which would be invalid if the document you created after the one you are currently on was deleted or moved to another location in the tree.
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Indeed; that was just a quickie idea. The PrevJumpNext snippet (modified if necessary) would be much better.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 6649
                • 25 Posts
                To everyone
                Thanks very much for all the help, and for showing me where the code goes and how its constructed.
                If someone can give me an example of using the ’Next’ feature as ’bunk58’ has with the ID+1 code I would also be very grateful.
                Thanks again

                Regards

                D grin
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  the NextJumpPrev snippet replaces your entire link. If you want to use the ID + 1 method, just make the snippet and put the snippet tags inside the href=" " part of the <a> tag.
                  <a href="[!snippetName!]">link text</a>

                  This is not a particularly stable method, as Jason pointed out, if a document is deleted or moved and the sequence is disturbed it will break.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 6649
                    • 25 Posts
                    Sottwell -

                    If I was to use the PJN snippet rather than the snippet within the <a> tag, how would this look. I have got the snippet in the database ready to go, but I’m having difficulty making it work within the template. Any chance you can give me an example like bunk58?

                    Thanks once again

                    Regards

                    D
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      Put the snippet tags at the top of each document’s content, or put it in your template above the content. Where you want the links to appear, just put the placeholders you want, such as [+PJN_prev+] or [+PJN_next+].

                      See the instructions in the snippet’s code about optional parameters to customize it.
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org