[[!getResources? &parents=`27` &limit=`1` &tpl=`testimonial` &includeContent=`1` &sortby=`RAND()`]]
Thought I’d share something useful I discovered with getResources. Not sure if this should be posted here or in the getResources forum; too bad you can’t cross-post or use tags...
The site I’m working on has a bunch of testimonials or quotes from happy customers. I wanted the ability to show a random one in the sidebar. You can do something like this with the RandomChunk add-on, but it requires a comma-separated list of chunks. Instead I put each quote in a separate resource and found a way to select one at random for the sidebar and also be able to list them all on a ’Testimonials’ page.
Since the sortby param is passed to the SQL you can use the MySQL ’RAND()’ function:
[[!getResources? &parents=`27` &limit=`1` &tpl=`testimonial` &includeContent=`1` &sortby=`RAND()`]]
The above will select a random resource that is a child of resource id 27. To list all the resources in random order, just change the limit to zero.
[[!Snippet? &prop1=`rand()`]] [[!Snippet? &prop1=`rand()`]]
[[!Snippet? &prop1=`rand()`]] [[!Snippet? &prop1=`rand()` &instance=`2`]]
[[!getResources? &parent=`2012` &tpl=`home_quote` &includeContent=`1` &sortby=`RAND()` &limit=`1` &instance=`2`]] [[!getResources? &parent=`2012` &tpl=`home_quote` &includeContent=`1` &sortby=`RAND()` &limit=`1` &instance=`3`]]
As described previously in this thread, if the tag signature is exactly the same as another tag on the same Resource, the value will come from the cache. The tag signature has to be unique.
(I tried adding the 'instance' property as opengeek suggested but behavior didn't change)
Also, those calls above are always cached, ie: they pick one resource from the parent directory and render that on every page request until I clear the site's cache.
Anyone know what's going wrong in this case or where I could learn more about how getResources, &sortby=`RAND()`, and modX caching work together?
Thanks!