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

Display recent blog posts on homepage?


Go to End


20 Posts   9780 Views

Avatar
mhdesign

Community Member, 216 Posts

1 July 2015 at 5:21pm

Sure there is a widget or something that can help me do this but I can't seem to find anything thing that works with later SilverStripe builds. Did find the following tutorial which seemed simple enough but it's not pulling latest posts -- can someone please advise where I put the 'function' on Page.php?

http://www.clickheredigital.co.uk/blog/include-a-silverstripe-blog-entry-on-any-other-page/

Or if anybody can recommend suitable widget that would also be cool. Thanks!

Avatar
Pyromanik

Community Member, 419 Posts

1 July 2015 at 8:49pm

Tutorial is old, for v2 of silverstripe.
Keep the function in the same place, but use code like:

return BlogPost::get()->sort('Date', 'desc')->first();

http://docs.silverstripe.org/en/3.1/developer_guides/model/data_model_and_orm/

Avatar
mhdesign

Community Member, 216 Posts

2 July 2015 at 9:44am

Hi Pyromanik, thanks for your comment. Wondered if it was a version problem but don't know enough PHP to tell!
I'm getting a fatal error -- can you advise if I'm inserting code in correct place? Thanks.

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
		
		// Note: you should use SS template require tags inside your templates 
		// instead of putting Requirements calls here.  However these are 
		// included so that our older themes still work
		
		Requirements::javascript(THIRDPARTY_DIR.'/jquery/jquery.min.js'); //include jquery 1.7.2 bundled with ss
		//Requirements::javascript('http://code.jquery.com/jquery-1.7.2.min.js');
		Requirements::javascript('themes/'. SSViewer::current_theme() .'/javascript/navscript.js'); //link to js file in current theme folder
	}
	
	function LatestBlogEntry($num=3) { 
    	$blogpost = DataObject::get_one("BlogHolder"); 
		return BlogPost::get()->sort('Date', 'desc')->first();
	}

Avatar
helenclarko

Community Member, 166 Posts

3 July 2015 at 10:29am

Hi mhdesign,

I'm sure I had this down in another post awhile back, guess the author pulled the post.

Under the Page_Controller in HomePage.php:

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

Homepage.ss

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

Avatar
helenclarko

Community Member, 166 Posts

3 July 2015 at 10:34am

Edited: 03/07/2015 10:35am

Just BTW,

If you want your current latestBlogEntry function to work, try returning blogpost rather than the process.

function LatestBlogEntry($num=3) { 
    	$blogpost = DataObject::get_one("BlogHolder"); 
		return blogpost;
	}

Avatar
mhdesign

Community Member, 216 Posts

3 July 2015 at 3:14pm

Hey helenclarko (should I call you 'helen'?), thanks for your response! Really clear and simple for non-programmer like myself! Only one final hiccup -- I have one post loaded in blog but it's not showing up -- instead I'm getting the 'There are currently no blog entries. Please check back again shortly.' message. Is there anything else that I needed to do?

Avatar
Pyromanik

Community Member, 419 Posts

3 July 2015 at 10:37pm

Make sure the post is published (not just saved [as a draft])

Avatar
mhdesign

Community Member, 216 Posts

4 July 2015 at 3:08pm

Great point Pyromanik (don't overlook the obvious!) -- but yes, the post IS published : )

Go to Top