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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Blog Module /

Discuss the Blog Module.

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

Blog entries on Home Page

Go to End

6 Posts   3769 Views


Community Member, 6 Posts

13 April 2009 at 12:57am

I have been trying for the past few day to get the latest Blog entries displayed in the sidebar. I just can't seem to get it to work. I have followed the news tutorial. I am using the Black Candy theme and the latest version of silverstripe. I have created a homepage. Can anybody help me with the code I need to add to each page?


Community Member, 215 Posts

13 April 2009 at 10:09am

You need to add a function to your homepage controller to pull the data from the blog post, I use this:

function LatestPost($num=2) {
  	$blogpost = DataObject::get_one("BlogHolder");
  	return ($blogpost) ? DataObject::get("BlogEntry", "", "Date DESC", "", $num) : false;

I am then able to use this in my homepage template:

<% control LatestPost %>
<li><a href="$Link" title="Read more on &quot;{$Title}&quot;">$Title</a></li>
<% end_control %>


Community Member, 1 Post

15 April 2009 at 3:21pm

I'm so completely new to SS, please forgive me but when you refer to adding the function to the homepage controller, where in the tree would I find the file responsible for this.


Community Member, 215 Posts

15 April 2009 at 3:36pm

Oh righto, firstly I recommend you do the first couple of tutorials But specifically for this issue:

If you are just using a standard page for your homepage then you will need to add that function into the /mysite/code/Page.php file in the page controller, in a similar fashion to this

But if you have made a specific HomePage type of page then that will have its own controller in HomePage.php where the function should be placed


Community Member, 14 Posts

23 April 2009 at 8:24am

What if I also wanted to retrieve the number of comments on the blog post?


Community Member, 14 Posts

23 April 2009 at 8:43am

Okay I figured out how to get the number of comments, but maybe someone could explain to me how exactly this works.

I did so by adding $Comments.Count into the template file and it simply worked. What I'm confused about is, how are the comments magically made available to SS when my query ($latestPost = DataObject::get_one('BlogEntry', '', false, 'Created DESC')) is only selecting the blog entry. If I print_r($latestPost), I don't see anything about comments.

I guess when the template is rendered, $Comments tells SS to select the data from the related table?