We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26681 MODX Staff
    • 123 Posts
    Hi all,

    After looking around for a modification to the beloved FirstChildRedirect snippet that will randomize the results, I found a snippet by Tom Dutczak here: http://www.tsmith.org.uk/modx-redirect-to-random-child.html

    Doesn’t seem to exist anywhere else in the forums, and if someone wanted to redirect to a random child resource from a container, this seems to do the trick. I’m a PHP moron, so if anyone sees something in this snippet that is critically flawed, please comment. Otherwise, consider using it for the (rare) cases where you need this.

    Cheers,
    Sepia River
      [sepiariver.com] (https://sepiariver.com/)
      • 22303 MODX Staff
      • 10,725 Posts
      FWIW, you could also just make the container a WebLink with a Snippet as the "content" to simply return the random child’s Resource id. Food for thought—much simpler and the resource is redirected before any other processing occurs...
        • 26681 MODX Staff
        • 123 Posts
        Thanks for this. As the aforementioned PHP moron, what would the snippet returning random child doc ID actually look like? tongue
          [sepiariver.com] (https://sepiariver.com/)
          • 4172
          • 5,888 Posts
          something like this could do that:

          <?php
          $docid = (isset($docid))? $docid: $modx->documentObject['id'];
           
          $children= $modx->getActiveChildren($docid, 'menuindex', 'ASC');
          if (!$children === false) {
              return $children[rand(0,count($children)-1)];
          } else {
              return $modx->config['site_start'];
          }
          
          ?>
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 26681 MODX Staff
            • 123 Posts
            Thanks. Seems the code posted:
            Quote from: Bruno17 at Mar 22, 2011, 03:23 PM

            something like this could do that:

            <?php
            $docid = (isset($docid))? $docid: $modx->documentObject['id'];
             
            $children= $modx->getActiveChildren($docid, 'menuindex', 'ASC');
            if (!$children === false) {
                return $children[rand(0,count($children)-1)];
            } else {
                return $modx->config['site_start'];
            }
            
            ?>


            exceeds the maximum number of characters (255) allowed in weblink field re: Quote from: OpenGeek at Mar 22, 2011, 02:45 PM

            FWIW, you could also just make the container a WebLink with a Snippet as the "content" to simply return the random child’s Resource id. Food for thought—much simpler and the resource is redirected before any other processing occurs...
            .

            Also, I don’t seem to be able to make it work calling the snippet from a container doc, as with Tom’s snippet mentioned above - although it’s likely I’m misunderstanding something or implementing something incorrectly. Tom’s solution was idiot-proof enough that I’ve got it working: sepiariverphotography.com

            Cheers, Sepia River
              [sepiariver.com] (https://sepiariver.com/)
              • 22303 MODX Staff
              • 10,725 Posts
              No I meant the Snippet tag...
                • 26681 MODX Staff
                • 123 Posts
                Ok, I think understand smiley

                1. I can call the snippet from a modDocument template as I did originally - worked fine.
                2. I can also call the snippet from the weblink field in a modWebLink resource - this also works, and apparently some performance advantages this way...is this pretty much what you were getting at?

                Neither of these worked with the snippet code posted in this thread. I’m still using the snippet code from Tom’s site. Bruno’s code returned a blank page with url: "domain.com/Array"..??

                Weird little bug...Chrome returns a 404 error when trying to view source code of the rendered page. Works fine in FF and Safari...probably just need a browser restart. Weird, I’ve never seen that...
                  [sepiariver.com] (https://sepiariver.com/)
                  • 22303 MODX Staff
                  • 10,725 Posts
                  The Snippet tag you enter in the WebLink target field (i.e. content) would just return the proper Resource id, not perform the redirect itself, as the RandomChildRedirect does. This would just be a RandomChild snippet—no redirect, since that’s what WebLinks already do.
                    • 26681 MODX Staff
                    • 123 Posts
                    OK. That part finally makes sense to me. Thanks for your patience. Being a PHP Moron though, I’m not sure I can implement a RandomChild snippet. Bruno’s code didn’t work for me, but it’s possible (likely) that I’m doing something wrong. If someone similarly proficient as I am wants this obscure feature, I might still suggest Tom’s redirect snippet. If they can implement it the way you suggest, I totally agree it makes more sense. After all, yes, a weblink IS a redirect.

                    On that note: after using MODX weblinks on my old site to redirect to my new site, I started getting obsessed with how Search Engines might interpret them. According to seo-browser.com, Googlebot DOES see a 301 Response code from my weblink resources!

                    Example report here: http://seo-browser.com/index.php?address=http%3A%2F%2Fsepiariver.com%2Fplaces.htm&button1.x=35&button1.y=12&button1=Advanced&user_agent=1&q=http%3A%2F%2Fsepiariver.com%2Fplaces.htm

                    I find this really interesting...no other mention that I could find in the forums nor Google about how Search Engines see MODX Weblinks and if any Response Codes are generated...????
                      [sepiariver.com] (https://sepiariver.com/)