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.

All other Modules

Discuss all other Modules here.

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

SOLVED! how to get Latest News on custom page and homepage


Reply

5 Posts   518 Views

Avatar
w1l

16 March 2013 at 12:46am (Last edited: 16 March 2013 12:48am), Community Member, 13 Posts

Hi there

I currently have latest news on the homepage, which is great, i want it on another page as well.

I've created a custom page type with a Controller 'HomePageNoIntro.php' and a Template 'HomePageNoIntro.ss'

I've copied the code from HomePage.php and HomePage.ss and placed in the new pages.

Which creates the container, but no articles ... I'm sure the issue is in the Controller page :mysite/code/HomePageNoIntro.php

<?php
class HomePageNoIntro extends Page {
}

class HomePageNoIntro_Controller extends Page_Controller {
// ...
   public function LatestNews($num=5) {
    $holder = ArticleHolder::get()->First();
    return ($holder) ? ArticlePage::get()->filter('ParentID', $holder->ID)->sort('Date DESC')->limit($num) : false;
   }
}


Do i need a different function from the HomePage.php function? - i've tried just changing the function name (LatestNewsB) - and updating the template ... but this did nothing?

I'm confused - surely this must be simple?

Thanks for reading.

Avatar
bartvanirsel

16 March 2013 at 5:00am Community Member, 94 Posts

Try putting the method on Page.php, presuming both Homepage and HomePageNoIntro extend Page.

Avatar
w1l

18 March 2013 at 9:48am (Last edited: 18 March 2013 9:56am), Community Member, 13 Posts

Many thanks bartvanirsel - both HomePage and HomePageNoIntro do extend Page -good thinking.

Unfortunately the result is exactly the same as previous ... items appear in the HomePage but not in HomePageNoIntro.

Errk.

Hmm i wonder might this problem be that the url path is different for these two pages.

HomePage = /
HomePageNoIntro = /home-alt

??

I don't know silverstripe code well enough, but i wonder if something in this line can't handle the change in path?

return ($holder) ? ArticlePage::get()->filter('ParentID', $holder->ID)->sort('Date DESC')->limit($num) : false;

Avatar
bartvanirsel

18 March 2013 at 7:51pm Community Member, 94 Posts

Hi, the line looks ok.

If the latestnews Method is in Page.php and both other page types exend it, this method should work.
The path does not matter.

Avatar
w1l

19 March 2013 at 10:25pm Community Member, 13 Posts

SOLVED!

The HomePageNoIntro.ss page was looping <% include NewsTeaser %> ... when it should have been <% include ArticleTeaser %>

So the Controller was fine as you said bartvanirsel. Thanks very much for your advice ... was pulling hair on this one!

/w