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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Randomly load ChildrenWithLimit

Go to End

3 Posts   768 Views


Community Member, 79 Posts

11 July 2009 at 4:15am

I am using the following code to pull 2 children of the StaffHolderpage into the Homepage.


function ChildrenWithLimit($limit = 2) { 
return DataObject::get('Page', "ParentID = $this->ID", '', '', $limit); 

<% control Page(about-us) %> 

  <ul id="StaffList">
   <% control ChildrenWithLimit %> 

        <div class="staffname"><a href="$Link">$Title</a></div>
        <div class="staffphoto">$Photo</div>
        <div class="staffdescription"><p>$Content.FirstSentence</p></div>
    <% end_control %>
<% end_control %>

But what I hope to do, is to randomly choose 2 of these. Is this possible? how would it be done? Any help would be much appreciated.


Community Member, 904 Posts

11 July 2009 at 5:28am

Using "RAND()" for the sorting should do the trick. Eg.

return DataObject::get('Page', "ParentID = $this->ID", 'RAND()', '', $limit); 


Community Member, 79 Posts

11 July 2009 at 9:02pm

Many Thanks for your help, this all seems to be working!