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.

Archive /

Our old forums are still available as a read-only archive.

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

How do I get the latest forum posts shown on the homepage?


Go to End


11 Posts   5514 Views

Avatar
Blackdog

Community Member, 156 Posts

24 April 2008 at 11:01pm

Edited: 24/04/2008 11:49pm

find your Page.php file which will be the basis of your homepage.. unless you have changed it.

add this after the function init() like below..

class Page_Controller extends ContentController {
	function init() {
	 ....
	}

/** 
* Get the latest posts from the forum, with a limit. 
* 
* @param Int $param The number of posts to retrieve 
* @return Post Returns the latest posting or nothing on no posts. 
*/ 
	function LatestPosts($limit) {
		$posts = DataObject::get("Post", "ForumID != 0", "Created DESC", "", $limit);
		return $posts;
	}

}

Avatar
Foweyboy

Community Member, 7 Posts

25 April 2008 at 11:04am

thanks for that advice - it's solved that error message but unfortunately it doesn't return any posts.

I've found there are 2 LatestPost functions, one in Forum.php and one in Post.php (both in forum/code).

Post.php:
function LatestPost() {
$filter = "";
if($this->ParentID != 0) {
$parents = $this->getDescendantIDList();
$parents[] = $this->ID;
$filter = "AND ParentID IN (" . implode(",", $parents) . ")";
}
$posts = DataObject::get("Post", "TopicID = $this->TopicID $filter",
"Created DESC", "", 1);
if($posts) return $posts->First();
}

Forum.php
/**
* Get the latest posting of the forum
*
* @return Post Returns the latest posting or nothing on no posts.
* @todo This is causing some errors, temporarily added is_numeric.
*/
function LatestPost() {
if(is_numeric($this->ID)) {
$posts = DataObject::get("Post", "ForumID = $this->ID",
"Created DESC", "", 1);
if($posts)
return $posts->First();
}
}

As you can see, they are similar but different.

And now I have 2 LatestPosts functions, one also in Forum.php and one in mysite/code/page.php as per the code posted in previous messages

So it compiles and runs, but doesn't actually produce any code in the resulting html. HELP!

Avatar
Blackdog

Community Member, 156 Posts

25 April 2008 at 11:31am

try taking the $limit variable out of the function and from your on page controller and set it manually to check it isn't that. Sorry, i didn't test it.

Go to Top