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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

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

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


Go to End


5 Posts   1726 Views

Avatar
w1l

Community Member, 13 Posts

16 March 2013 at 12:46am

Edited: 16/03/2013 12:48am

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

Community Member, 96 Posts

16 March 2013 at 5:00am

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

Avatar
w1l

Community Member, 13 Posts

18 March 2013 at 9:48am

Edited: 18/03/2013 9:56am

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

Community Member, 96 Posts

18 March 2013 at 7:51pm

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

Community Member, 13 Posts

19 March 2013 at 10:25pm

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