Dallby
Dallby

Reputation: 596

SilverStripe: Latest Blog Posts Content Block

I'm using SheaDawson's blocks module, and I'm trying to create a "latest blog posts" content block. Here is my DataObject:

<?php

class LatestBlogPosts extends Block {
    private static $db = array(
        'ContainInGrid' => 'Boolean',
        'PostCount' => 'Int'
    );

    static $defaults = array(
        "PostCount" => 2
    );

    function getCMSFields() {
        $fields = parent::getCMSFields();
        return $fields;
    }

    public function LatestPosts() {
        $blog = DataObject::get("BlogEntry", "", "Date DESC", "", $this->PostCount);
        return $blog;
    }
}

On the page template it's not displaying any posts. It says it can't find any. When I checked the database the BlogEntry table is empty, even though I have two posts that are published.

How do I fix this issue?

Upvotes: 2

Views: 82

Answers (1)

3dgoo
3dgoo

Reputation: 15794

In the latest version of the SilverStripe blog module the blog entry class is named BlogPost. BlogEntry is what the class used to be, but this changed sometime in 2015.

If you are using the latest version of the blog module your blog entries will be created as BlogPosts and that data will be in the BlogPost database table.

Your LatestPosts function should look like this:

public function LatestPosts() {
    return BlogPost::get()->sort('Date', 'DESC')->limit($this->PostCount);
}

Upvotes: 4

Related Questions