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

fetching news from multiple pagetypes


Reply

3 Posts   723 Views

Avatar
kirillis

10 December 2010 at 1:01pm Community Member, 2 Posts

hi,

I just finished the tutorials and now I´m stuck with my news page. I want to have multiple pages from which the main homepage-news should get the newest articles. at the moment, it only gets the 5 newest articles from the articleHolder and ArticlePage pages. I think, I have to modify the homepage.php, at the moment it looks like this:

<?php
/**
* Defines the HomePage page type
*/

class HomePage extends Page {
static $db = array(
);
static $has_one = array(
);
static $icon = "themes/tutorial/images/treeicons/home";

}

class HomePage_Controller extends Page_Controller {
function LatestNews($num=5) {
$news = DataObject::get_one("ArticleHolder");
return ($news) ? DataObject::get("ArticlePage", "ParentID = $news->ID", "Date DESC", "", $num) : false;
}

}
?>

I was thinking to just add another function but I don´t know if that would work. Also, it always should get the 5 newest from all categories/pages, not from each page.. could someone help me with this?

Avatar
Willr

10 December 2010 at 9:13pm Forum Moderator, 5511 Posts

Also, it always should get the 5 newest from all categories/pages

Any pages? usually you don't want every single page included (i.e homepages) but if you wanted then you just get the root 'Page' objects.

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

That also implies you have added a Date field to your Page.php file.

If you want to join multiple types in 1 LatestNews function then you can use multiple DataObject::get() calls then use merge() and getRange() to ensure you only get 5 (when you merged 5 + 5 = 10)

(http://api.silverstripe.org/2.4/sapphire/model/DataObjectSet.html)

function LatestNews($num=5) {
$articles = DataObject::get("Article", "", "Date DESC", "", $num);
$blogs = DataObject::get("BlogEntries", "", "Date DESC", "", $num);

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

Avatar
kirillis

10 December 2010 at 11:11pm Community Member, 2 Posts

hi willr,

yes, i wanted to only get news from certain page types, not all pages.

so, to get news from 3 sources i could use this?

function LatestNews($num=5) {
$articles = DataObject::get("Article", "", "Date DESC", "", $num);
$blogs = DataObject::get("BlogEntries", "", "Date DESC", "", $num);
$source3 = DataObject::get("PageTypesource3", "", "Date DESC", "", $num);

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

thanks, will try :D