Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » How do I get the latest forum posts shown on the homepage?

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

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

Page: 1 2
Go to End
Author Topic: 3902 Views
  • Blackdog
    Avatar
    Community Member
    156 Posts

    Re: How do I get the latest forum posts shown on the homepage? Link to this post

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

    }

  • Foweyboy
    Avatar
    Community Member
    7 Posts

    Re: How do I get the latest forum posts shown on the homepage? Link to this post

    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!

  • Blackdog
    Avatar
    Community Member
    156 Posts

    Re: How do I get the latest forum posts shown on the homepage? Link to this post

    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.

    3902 Views
Page: 1 2
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.