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.

Data Model Questions

[SOLVED] Multiple News


Reply

2 Posts   483 Views

Avatar
Craftnet

21 October 2011 at 8:02am (Last edited: 24 October 2011 5:56am), Community Member, 58 Posts

Hi,
I try to do multiple news consisting of News, "NewsPage" and the news "DiscographyNews".

I would like to be displayed by date.
At the moment I found such a thing

function LatestNews($num=10) {
   return DataObject::get("ArticlePage", "", "Date DESC", "", $num);   
}

Everything is good because it displays each note of the layout Articlepage (anywhere I would not put it)
When I try insert this code:

function LatestNews($num=10) {
   return DataObject::get("DiscographyPage", "", "Date DESC", "", $num);   
}

displays all DiscographyPage

I would like to combine it into one to sort by date and display all at once on the home page

I tried this:

function LatestNews($num=10) {
   return DataObject::get("ArticlePage", "", "Date DESC", "", $num);
return DataObject::get("DiscographyPage", "", "Date DESC", "", $num);      
}

But then retrieves only the first "return".

It can be somehow unite?

I find that but this not sort by date

function LatestNews($num=15) {
$articles = DataObject::get("ArticlePage", "", "Date DESC", "", $num);
$disc = DataObject::get("DiscographyPage", "", "Date DESC", "", $num);

$articles->merge($disc);
return $articles->getRange(0, $num);
}


Struck-out text because: See post below

Sorry for my bad English

Avatar
Craftnet

24 October 2011 at 5:53am Community Member, 58 Posts

I answer myself but it may be useful to someone.

I found a solution.
In function LatestNews need to insert such a thing

function LatestNews($num=15) {
   $sorcik = new DataObjectSet();
   $sorcik->merge(DataObject::get("ArticlePage", "", "Date DESC", "", $num));
   $sorcik->merge(DataObject::get("DiscographyPage", "", "Date DESC", "", $num));
   $sorcik->merge(DataObject::get("BiographyPage", "", "Date DESC", "", $num));
   $sorcik->sort('Date', 'DESC');
   return $sorcik->getRange(0, $num);
}

Now function LatestNews collects all the news of the code pages (in my case: ArticlePage, DiscographyPage and BiographyPage) and sorts them by date.

Sorry for my bad English