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 Headlines on HomePage with SilverStripe 3.1.x


Go to End


14 Posts   3466 Views

Avatar
AdamDimech

Community Member, 6 Posts

2 January 2015 at 8:13pm

Is there a simple explanation for how one can add x number of blog headlines to the homepage of a SilverStripe 3.1.x installation?

I have searched and searched and absolutely nothing works.

What goes into homepage.ss?
What goes into homepage.php?

It is almost beyond belief that this isn't written up somewhere. Any help would be most appreciated.

If someone has solved this, please share....

Avatar
helenclarko

Community Member, 166 Posts

13 January 2015 at 1:29pm

Hi AdamDimech,

I might be able to answer this for you, but first you should look into Datamodels and how to pull data from your SQL database.

http://doc.silverstripe.org/framework/en/topics/datamodel

Then you could do something like:

HomePage.php

	function latestBlog(){
	$blog = DataObject::get("BlogEntry", "", "Date DESC", "", 2);		
	return $blog;	
	}

Which will pull the latest 2 blog posts.

Then inside your HomePage.ss file
you will need to add a loop for the latestBlog:

<% loop latestBlog %>

Within the loop you should be able to pull data from the Blog table in your SQL database.
For example, you can inside the loop use "$Content" to pull the content for both blog posts.
Or even use "$Content.LimitWordCount(25)" (Limit to 25 words) and then add a link to the full blog post with a Read more link.

Hope that helps.

Regards,
-helenclarko

Avatar
AdamDimech

Community Member, 6 Posts

12 February 2015 at 10:53am

That link doesn't work. I found this but it makes almost no sense to me: http://api.silverstripe.org/3.1/class-DataModel.html

I tried what you have suggested and simply see an empty space where the blog posts should be.

This is my HomePage.php:

<?php
class HomePage extends Page {
}
class HomePage_Controller extends Page_Controller {
}
function latestBlog(){
$blog = DataObject::get("BlogEntry", "", "Date DESC", "", 2);		
return $blog;	
}

In my HomePage.ss:

<% loop latestBlog %>
	$Content
<% end_loop %>

What is missing?

Avatar
helenclarko

Community Member, 166 Posts

19 February 2015 at 3:08pm

Hi AdamDimech

Sorry, I probably didnt give you a very good example.
For a start, you have closed your HomePage_Controller class without the latestBlog function inside.

Here is how I have it set out:

HomePage.php

class HomePage_Controller extends Page_Controller {
	//Function for the latest Blog Post
	function latestBlog(){
	$blog = DataObject::get("BlogEntry", "", "Date DESC", "", 2);		
	return $blog;	
	}
}

HomePage.ss

			<div id="blogTitle">
				Latest Blog Posts
			</div>
			<div id="latestNews">
				<% if latestBlog %>
					<% loop latestBlog %>
						<% if Last %>
							<hr />
						<% end_if %>
						<div class="newsWrap">
							<a href="$Link" title="Read more" alt="Read more" class="newsTitle">$Title</a>
							<p><span class="dtstart">$Date.Long</span> from $Author<br />
							<br />
							<p>$Content.LimitWordCount(25) <a href="$Link" title="Read more" alt="Read more">More</a></p>
						</div>
					<% end_loop %>
				<% else %>
					<div class="newsWrap">
						<p>There are currently no blog entries. Please check back again shortly.</p>
					</div>
				<% end_if %>
			</div>

This is what I'm using.
Ignore the "latestNews" classes, as I was previously using this for pulling news articles rather than blog posts.
I'm simply keepin it there for the css links.

-helenclarko

Avatar
Fairfax71

Community Member, 20 Posts

12 June 2015 at 6:55pm

Edited: 12/06/2015 6:57pm

Hi all,

I am trying to do as you suggest, but even after a /dev/build?flush=1, my changes in the template don’t work.

Here is my complete /mysite/code/Page.php:

<?php
class Page extends SiteTree {

private static $db = array(
);

private static $has_one = array(
);

}
class Page_Controller extends ContentController {

/**
* An array of actions that can be accessed via a request. Each array element should be an action name, and the
* permissions or conditions required to allow the user to access it.
*
* <code>
* array (
* 'action', // anyone can access this action
* 'action' => true, // same as above
* 'action' => 'ADMIN', // you must have ADMIN permissions to access this action
* 'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
* );
* </code>
*
* @var array
*/
private static $allowed_actions = array (
);

public function init() {
parent::init();
// You can include any CSS or JS required by your project here.
// See: http://doc.silverstripe.org/framework/en/reference/requirements
}

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

public function UsersIpAddress() {
return $this->request->getIP();
}

}

Then in Page.ss I have:

<% loop LatestPost %>
<p>You are coming from $UsersIpAddress.</p>
<ul id="NewsList">
<li class="newsDateTitle"><span class="date">Date $Date</span><a href="$Link" title="Lesen Sie mehr über &quot;{$Title}&quot;">Title $Title</a></li>
</ul>
<% end_loop %>
</ul>
</div>

But it doesn't output anything from the data model, just this HTML:

<div id="aktuelles">
<p>You are coming from .</p>
<ul id="NewsList">
<li class="newsDateTitle"><span class="date">Date </span><a href="" title="Lesen Sie mehr über &quot;&quot;">Title </a></li>
</ul>

</ul>
</div>

The IP address function was just to see if it was working at all, which it isn’t. I thought maybe I was editing the wrong file, but my changes do show up, just the controller isn’t outputting the loop. I must be missing something obvious.

Um…help?

Cheers,

John

Avatar
Pyromanik

Community Member, 419 Posts

13 June 2015 at 12:05am

Edited: 13/06/2015 12:09am

You've read some kind of well outdated tutorial on this, all of your controller code in that function is deprecated.

You want: return BlogEntry::get()->limit($num);
If you want to ensure the date, then return BlogEntry::get()->sort('Date', 'DESC')->limit($num);
In both cases you will either get the latest blog post, or a null record.

Avatar
Fairfax71

Community Member, 20 Posts

14 June 2015 at 9:36pm

Hi,

thanks for your reply! Well, that explains a lot…

I tried updating the code, but everything between <% loop LatestPost %> and <% end_loop %> simply doesn’t show up at all now…?

Here is the code in Page.ss:

<% loop LatestPost %>
<ul id="NewsList">
<li class="newsDateTitle"><span class="date">Date $Date</span><a href="$Link" title="Lesen Sie mehr über &quot;{$Title}&quot;">Title $Title</a></li>
</ul>
<% end_loop %>

And here is the current code:

function LatestPost($num=5) {
return BlogEntry::get()->sort('Date', 'DESC')->limit($num);
}

Thanks in advance,

John

Avatar
Pyromanik

Community Member, 419 Posts

16 June 2015 at 10:05pm

So you need to debug the problem.
Although not particularly recommended amongst the hardcore developers, the easiest way to do that is to change to:

$return = BlogEntry::get()->sort('Date', 'DESC')->limit($num);
die(var_dump($return->count(), $return));
return $return;

Then reload your page.

Go to Top