We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • http://modxcms.com/forums/index.php/topic,29764.0.html

    Create this snippet and edit it. Set path and image sizes. Call it uncached. [[!RandomImage]]
    Works like a charm.
      • 18988
      • 3 Posts
      Hi,

      I also wanted to display a random image with GalleryItem snippet (from any album).
      So I changed few lines in GalleryItem snippet as I didn’t want to wait until the next update wink

      .....

      /* get ID of item */
      if($id !=’random’) { $id = (int)$modx->getOption(’id’,$scriptProperties,false); }
      if ($modx->getOption(’checkForRequestVar’,$scriptProperties,true)) {
      $getParam = $modx->getOption(’getParam’,$scriptProperties,’galItem’);
      if (!empty($_REQUEST[$getParam])) { $id = (int)$_REQUEST[$getParam]; }
      }
      if (empty($id)) return ’’;

      /* setup default properties */
      $tpl = $modx->getOption(’tpl’,$scriptProperties,’galItem’);
      $toPlaceholders = $modx->getOption(’toPlaceholders’,$scriptProperties,true);
      $toPlaceholdersPrefix = $modx->getOption(’toPlaceholdersPrefix’,$scriptProperties,’galitem’);
      $albumTpl = $modx->getOption(’albumTpl’,$scriptProperties,’galItemAlbum’);
      $albumSeparator = $modx->getOption(’albumSeparator’,$scriptProperties,’, ’);
      $albumRequestVar = $modx->getOption(’albumRequestVar’,$scriptProperties,’galAlbum’);
      $tagTpl = $modx->getOption(’tagTpl’,$scriptProperties,’galItemTag’);
      $tagSeparator = $modx->getOption(’tagSeparator’,$scriptProperties,’, ’);
      $tagSortDir = $modx->getOption(’tagSortDir’,$scriptProperties,’DESC’);
      $tagRequestVar = $modx->getOption(’tagRequestVar’,$scriptProperties,’galTag’);
      /* get item */
      $c = $modx->newQuery(’galItem’);
      $c->select($modx->getSelectColumns(’galItem’,’galItem’));
      if($id != ’random’) {
      $c->where(array(’id’ => $id)); }
      else {
      $c->sortby(’RAND()’,’ASC’);
      $c->limit(1); }


      $item = $modx->getObject(’galItem’,$c);
      if (empty($item)) return ’’;

      ....



      And it works with :
      [[!GalleryItem? &id=`random` ]]

      +++
      Vincent

      (tested with Revo 2.1.1 with Gallery 1.2.1)
        • 28398
        • 0 Posts
        Hi!
        I’ ve tested mediaandco code with MODX Revolution 2.1.1-pl and gallery 1.2.1 too, and it didn’t work for me. It’s look like on word ’random’ snippet making something wrong ?
          • 31955
          • 84 Posts
            • 37286
            • 160 Posts
            For anyone that just wants to grab a random thumbnail, I created a chunk called "myRandomThumb" with the below code:
            <div class="[[+cls]]">
              <img class="[[+imgCls]]" src="[[+thumbnail]]" alt="[[+name]]" />
            </div>
            

            and called from my template:
            [[!Gallery? &album=`1` &sort=`rand` &toPlaceholder=`0` &limit=`1` &thumbWidth=`210` &thumbHeight=`190` &thumbTpl=`myRandomThumb`]]
            

            Simple and easily expanded upon. Hope this helps whoever else comes along.
            • hello,
              is there some working solution to display random image from any album?
              Kristalin
                palma non sine pulvere
                • 37286
                • 160 Posts
                On my previous post I had stated that I had it working. Now, not so much. I am in the process of moving several websites to a new server and decided to upgrade revo to a newer version while I was at it. It took some time to even realize that it stopped rotating the image.

                Old setup:
                Revo 2.1.3
                Gallery 1.4.0

                New setup:
                Revo 2.2.5
                Gallery 1.5.2

                I don't know what changed...
                • come on guys give some solution on this problem, please!
                    palma non sine pulvere
                    • 37286
                    • 160 Posts
                    If this helps, my code posted a year ago does work, it will grab a random image. I believe the issue I am having is a caching issue. If I clear the website cache, the code will grab a completely different image on the next page load. I have still not found what is causing revo2.2.5 to hold onto the generated image instead of grabbing a different one on every page load.
                      • 4172
                      • 5,888 Posts
                      do you have this snippet-call directly on your template, or do you have it in another chunk?

                      Not sure, if it can be an issue, if you have your snippet wrapped in a cached chunk.
                      I think, I had lastly an issue like that.
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!