Jump to:

1840 Posts in 1563 Topics by 557 members

Blog Module

SilverStripe Forums » Blog Module » Blog entries on Home Page

Discuss the Blog Module.

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

Page: 1
Go to End
Author Topic: 2826 Views
  • al5976
    Avatar
    Community Member
    6 Posts

    Blog entries on Home Page Link to this post

    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?

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: Blog entries on Home Page Link to this post

    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 %>

  • Timmmy
    Avatar
    Community Member
    1 Post

    Re: Blog entries on Home Page Link to this post

    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.

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: Blog entries on Home Page Link to this post

    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

  • 1nsane
    Avatar
    Community Member
    14 Posts

    Re: Blog entries on Home Page Link to this post

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

  • 1nsane
    Avatar
    Community Member
    14 Posts

    Re: Blog entries on Home Page Link to this 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?

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