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.

Blog Module /

Discuss the Blog Module.

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

Blog entries on Home Page


Go to End


6 Posts   4463 Views

Avatar
al5976

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?

Avatar
Howard

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 %>
<ul>
<li><a href="$Link" title="Read more on &quot;{$Title}&quot;">$Title</a></li>
<li>$Date.Nice</li>
<li>$Content.FirstSentence</li>
</ul>
<% end_control %>

Avatar
Timmmy

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.

Avatar
Howard

Community Member, 215 Posts

15 April 2009 at 3:36pm

Oh righto, firstly I recommend you do the first couple of tutorials http://doc.silverstripe.com/doku.php?id=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 http://pastie.org/446929

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

Avatar
1nsane

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?

Avatar
1nsane

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?