Jump to:

23358 Posts in 17874 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » fetching news from multiple pagetypes

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

Page: 1
Go to End
Author Topic: 697 Views
  • kirillis
    Avatar
    Community Member
    2 Posts

    fetching news from multiple pagetypes Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: fetching news from multiple pagetypes Link to this post

    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)
    }

  • kirillis
    Avatar
    Community Member
    2 Posts

    Re: fetching news from multiple pagetypes Link to this post

    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

    697 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.