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.

General Questions /

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

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

show article with an option checked


Reply


2 Posts   919 Views

Avatar
cbartgu

Community Member, 12 Posts

17 June 2009 at 11:12pm

Does anyone know how to show an article which has a boolen option ticked, so if you had to pick a random article that it about a certain subject to show on the front page.

I wanted to use the <% control %> method but cannot find a tutorial on how to do it,

cbartgu,

Avatar
banal

Community Member, 901 Posts

18 June 2009 at 2:47am

The easiest way would be to create a custom method that will return a random article.
Example:

// put this in your Page_Controller
public function RandomArticle(){
   return DataObject::get_one('ArticlePage', 'Option = 1', true, 'RAND()');
}

Where 'ArticlePage' is the name of your Article Page Class and 'Option = 1' would be the WHERE statement.
Then in the Template you could write:

<% control RandomArticle %>
... access members of the ArticlePage here ...
<% end_control %>