Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Displaying random pages in homepage

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1063 Views
  • anujkryadav
    Avatar
    Community Member
    30 Posts

    Displaying random pages in homepage Link to this post

    hello
    i have created a page holder and a child page type,i am displaying random child in the holder page and it is working fine( got help from one of the thread) but now i need to display random 3 child in my homepage,help me out in doing that ,my code for the pages are

    TouristplaceHolder.php

    <?php
    class TouristplaceHolder extends Page {
    static $db = array(
    );
    static $has_one = array(
    );

    }

    class TouristplaceHolder_Controller extends Page_Controller {

    public function RandomChildren($max = 3)
    {
    return DataObject::get("TouristPlace","ParentID = $this->ID","RAND()",null,"0,$max");
    }
    }

    ?>

    TouristPlace.php

    <?php
    class TouristPlace extends Page {
    static $db = array(
    );
    static $has_one = array(
    'Photo' => 'Image'
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Content.Images", new ImageField('Photo'));
    return $fields;
    }

    static $defaults = array(
    'ShowInMenus' => false
    );

    }

    class TouristPlace_Controller extends Page_Controller {

    }
    ?>

    TouristplaceHolder.ss

    <% control RandomChildren %>
    Random title: $Title
    Photo:$Photo
    <% end_control %>

    the above code is working fine for me .what i need to do is to display random 3 TouristPlace pages in my home page

    thank you in advance

  • Rishi
    Avatar
    Community Member
    97 Posts

    Re: Displaying random pages in homepage Link to this post

    Check out similar to this is already been solved i think

  • nakashu
    Avatar
    Community Member
    24 Posts

    Re: Displaying random pages in homepage Link to this post

    I main think to the code to work also for your homepage is adding the the random function into page controller or homepage controller.

    public function RandomChildren($max = 3)
    {
    return DataObject::get("TouristPlace","ParentID = $this->ID","RAND()",null,"0,$max");
    }

    should work this way.
    It because when you have the function inside the TouristplaceHolder.php it is accessible only from that pagetype and pages extending it..
    But if you'll add it inside Homepage.php it will accessible also from that pagetype and pages extending it.

    And as most pages extend your page.php and page_contoller.. if you add it inside page_controller .. any page that extends page will have access to that function and you would be able to use it site wide.

    hope it solved the problem. . take care..


    p.s.: some info on the topic can be extracted from here:
    http://www.ssbits.com/create-a-static-sidebar/
    and these two you know probably:
    http://www.ssbits.com/getting-objects-from-multiple-child-pages/
    http://www.ssbits.com/displaying-fields-from-a-set-of-pages-on-another-page/

    1063 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.