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?
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.
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 "{$Title}"">$Title</a></li>
<li>$Date.Nice</li>
<li>$Content.FirstSentence</li>
</ul>
<% end_control %>
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.
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
What if I also wanted to retrieve the number of comments on the blog post?
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?