Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS

Display Content from Random child Page (based on Count)

Go to End

10 Posts   3141 Views


16 July 2009 at 10:37am (Last edited: 16 July 2009 10:39am), Community Member, 99 Posts

Hi there,

I really need help here before I go mad!

On the left hand side of all my templates I want to display a small box with a random client comment.


In the CMS I have set up a "Holder" page with the URL/ID "client-feedback-items". (This is a blank page)

The children of this holder use a page type which has three custom fields:

Quote (text)
Name (text)
Organisation (text)

I am using the following naming convention for each new child page: clientFeedback1, clientFeedback2, clientFeedback3 and so on.

The pages themselves are never displayed within the site structure (they are hidden from menu and search). Instead I want to display elements from them on most other pages (randomly picking one)

I have an include, called by most pages in my site, named

This contains the following code at the moment:

<div id="quote">
<% control Page(clientFeedback1) %>
<p class="words">"$Quote"</p><p class="person">$Name, <br/>$Organisation</p>
<% end_control %>

This allows me to display the content of the first quote page.
If I changed it to <% control Page(clientFeedback2) %> it would display the contents of that page instead.
(This works so far...)

STEP 2) this is where I am coming undone!!!

I need to know how many child pages there are in the holder "client-feedback-items", then i need to pick a random number between 1 and the 'child count' of "client-feedback-items". I then have to return this value to the template page in order to display the random quote.

Here's how I've tried to go about it:

In the Page.php controller I added the following code: (idea was to count child pages then return a random number)

function pickRandomQuote() {
      $upperLimit = $Children(client-feedback-items).Count; //count number of child items
      $random = rand(1, $upperLimit);
      $theRandomQuote = 'clientFeedback'.$random;
      return $this->$theRandomQuote;

then I changed the code in as follows:

<div id="quote">
<% control Page($pickRandomQuote) %>
<p class="words">"$Quote"</p><p class="person">$Name, <br/>$Organisation</p>
<% end_control %>

(Replacing the hard-coded ID with the variable/funtion: $pickRandomQuote)

For reason(s) that I don't understand it doesn't work. It's maybe something a bit obvious and stupid that I'm just not seeing (well, it's getting late now and I'm losing the will to go on!)

I also tried using:
$upperLimit = $Children.Count(client-feedback-items); //count number of child items

and just a hard-coded one:
$random = rand(1, 4);

to test if it worked or if the count code was wrong, but it still didn't work.

Any help would be much appreciated. I know I'm waffling on a bit, but its hard to explain it all!!!




4 August 2009 at 1:12am Community Member, 99 Posts

Just bumping this up in the hope that someone can point me in the right direction...?


4 August 2009 at 10:14am Community Member, 57 Posts


I *hope* this is what you're after (to go in the Page Controller and called by $RandomChild on the template):

function RandomChild()
   // This returns 1 object (last argument) of QuotePages (your group of quotes)
   // and the object returned is ordered randomly (RAND()) so it'll be random!
   $randomChild = DataObject::get("QuotePages", null, "RAND()", null, 1);
   return $randomChild;

In the template you should then do a control:

<% control RandomChild %>$Quote by $Author<% end_control %>

I must say that this would be much more efficient if you had a data-object of quotes rather than full pages. This tutorial goes into it really well:

I hope this helps :)


4 August 2009 at 8:40pm Community Member, 99 Posts

Thanks very much for your suggestions.

I'll try it out and I'll also have a look at the alternative version too. Need to do a bit more reading on this: am just trying to learn on the fly while I develop my first couple of SS sites, so I am probably missing a lot of cool functionality!




4 August 2009 at 8:46pm Community Member, 57 Posts

Let me/us know how you get on :)


12 September 2009 at 2:10am Community Member, 99 Posts

Hi there,

Sorry for the delay in getting back to you. Project was on hold for a wee while.

I still can't get this to work. Keep getting errors and not sure how to fix them...

Am having a look at the data object info just now but i think its going over my head!!!


12 September 2009 at 2:25am Community Member, 99 Posts

I take it back - I've just worked it out!!!

In this part:
$randomChild = DataObject::get("QuotePages", null, "RAND()", null, 1);

I was setting the $obj (which you had called QuotePages) to the name of the parent page, whereas it should have been the PageType!


Thank you!



22 October 2009 at 9:16am Forum Moderator, 435 Posts

... and if you want last 3...

class HomePage_Controller extends Page_Controller {
   // Get random object
function getRandomProductObject() {
return DataObject::get('Product', null, 'RAND()',null,$limit=3);



Go to Top