We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6814
    • 25 Posts
    I’m trying to set up a page with a random background image drawn from a tv in one of my "background" documents. Easy enough using Ditto and a simple tpl chunk. Elsewhere on the page, however, I’d like to be able to link to the page that includes the full image (used in the background) and a description of that photo. The trick for me is that I want the background image to be selected randomly from all of the available pages.

    I can randomize the output for the background, but is it possible to store that value so that I can then link to the corresponding document later in the page?

    Thanks for any suggestions.
      • 6814
      • 25 Posts
      Maybe I’m doing this wrong.

      Here’s a page that exemplifies what I’d like to do http://nmhistorymuseum.unparalleldesign.com/hours-and-admission.html. The background image in the header is drawn randomly from the documents that have "Backgrounds" as a parent.

      The link at the bottom of the content area is directed to the full page description of that image.

      I’d like to be randomly selected, but if that impossible (within my skill set) I could explicitly set the background image through a tv.

      Hopin’ I don’t have to.
        • 6814
        • 25 Posts
        here’s how I addressed this.

        1. Call a custom snippet, get_rand_doc, that selects a document at random from those that have ’backgrounds’ as a parent. Set its ’id’ as a session variable.
        2. Call Ditto. Using the ’documents’ parameter set to the session variable to return the background image.
        3. Call Ditto again in the same way but with a different tpl chunk to return the link to the full page.
        4. Call another custom snippet that unsets the session variable.

        Seems kind of sloppy. If anyone has any other suggestions, I’d appreciate, but for now, it’s done.
        • Can’t you just use your random selection snippet as a parameter to the GetField snippet?
            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
            • 23571
            • 223 Posts
            I would probably use the randomDoc snippet in that instance (below). It randomly returns a document in a directory and you then have any of those doc fields available to you. Hope that helps.

            <?php
            // snippet RandomDoc
            // returns content from a random document 
            // from a given folder
            // usage [!RandomDoc?folder=`xxx`!]
            // if $folder is not set, uses current doc ID as folder
            $folder = isset($folder)?$folder:$modx->documentIdentifier;
            $docs = $modx->getActiveChildren($folder);
            // get a random document id
            $rnd = rand(1, count($docs));
            $rnd -=1; // because we need to start at 0 in the array
            $randomDocId = $docs[$rnd]['id'];
            // get the content of the random doc
            $contentRow = $modx->getPageInfo($randomDocId,1,'link_attributes,pagetitle, introtext,id');
            $ret = '';
            $ret .= '<span id="verse-title">'.$contentRow['pagetitle'].'</span><br />';
            $ret .= '<span id="verse-content">'.$contentRow['introtext'].'</span>';
            $ret .= '';
            return $ret;
            ?>
              • 6814
              • 25 Posts
              Much thanks for both suggestions. I will give each a try.