We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51382
    • 53 Posts
    Hi, I'd like to generate a random link function much like Wikipedia offers. Can someone assist? There are about 2-3 posts on this from 3-6 years ago, but I cannot replicate them.

    I am below average in my tech skills, so please dumb it down.

    From what I read, getResources can produce this.

    On one page I have a link:
    <a href="/test/randompage">Random</a>


    On randompage I have the getResources call
    [[!getResources? &sortby=`RAND()` &limit=`1`]]


    I want the full resource, but is a `tpl` I have to call as well or something else I am missing?

    Thank you

    This question has been answered by BobRay. See the first response.

      • 17301
      • 932 Posts
      Yeah you'll need to specify a template and where to get the resources from. For example here's a piece of code I use on a site to get a bunch of random recipes. It's using getPage rather than getResources but it's the same thing more or less smiley just specify a tpl and then create a chunk on how you want it to output.

      [[!getPage? 
        &element=`getResources` 
        &parents=`157` 
        &tpl=`myRecipesTpl`  
        &showHidden=`0`
        &includeTVs=`1` 
        &processTVs=`1`
        &limit=`4`
        &sortby=`RAND()`
      ]]
        ■ email: [email protected] | ■ website: https://alienbuild.uk

        The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
        • 51382
        • 53 Posts
        Thanks for that.

        I should have specified that I am looking to return a full resource. Is there a way to do that?
          • 17301
          • 932 Posts
          Do you mean redirect to a random resource on click?
            ■ email: [email protected] | ■ website: https://alienbuild.uk

            The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
            • 51382
            • 53 Posts
            Quote from: lkfranklin at Sep 22, 2016, 10:51 AM
            Do you mean redirect to a random resource on click?

            Yes
              • 17301
              • 932 Posts
              Never done it before but I would imagine you could use this as it supports random child redirection
              http://modx.com/extras/package/redirectoid

              Include the snippet on an empty resource and then have your 'random' button or link point to it.

                ■ email: [email protected] | ■ website: https://alienbuild.uk

                The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
              • discuss.answer
                • 3749
                • 24,544 Posts
                You shouldn't need a separate resource. Try this:

                <a href="[[!getResources? &tpl=`RandomTpl` &sortby=`RAND()` &limit=`1`]]">Random</a>


                Then create a Tpl chunk called RandomTpl with just this code:

                [[~[[+id]] ]]
                  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
                  • 51382
                  • 53 Posts
                  Quote from: BobRay at Sep 23, 2016, 03:40 PM
                  You shouldn't need a separate resource. Try this:

                  <a href="[[!getResources? &tpl=`RandomTpl` &sortby=`RAND()` &limit=`1`]]">Random</a>


                  Then create a Tpl chunk called RandomTpl with just this code:

                  [[~[[+id]] ]]

                  Thanks Bob, I will try this tonight.

                  And thanks LK as well.
                    • 51382
                    • 53 Posts
                    It worked!