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
Pyromanik

Community Member, 419 Posts

5 July 2015 at 6:42am

Edited: 05/07/2015 6:44am

The last example given confuses BlogHolder and BlogEntry (and also always only gets one, not $num).

Try:

public function LatestBlogPosts($num=3) {
    return BlogPost::get()->sort('PublishDate','desc')->limit($num);
}

(Assuming you're using: http://addons.silverstripe.org/add-ons/silverstripe/blog )

Avatar
helenclarko

Community Member, 166 Posts

6 July 2015 at 8:59am

Hi Mhdesign,

If you are still experiencing issues, can you post your code for us to take a look at?

Regards,
-helenclarko

Avatar
mhdesign

Community Member, 216 Posts

6 July 2015 at 9:12am

Sorry guys, neither version is giving the desired result! And yes, I am using the standard SilverStripe blog module.

Page.php code follows. Not sure if I've put it in the right place but...

<?php
class Page extends SiteTree {

	private static $db = array('SidebarHTML' => 'HTMLText'
	);

	private static $has_one = array(
	);
	
function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields -> addFieldToTab("Root.Sidebar",new HTMLEditorField("SidebarHTML"));
		return $fields;
	}
	
}

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() {
		
		//Function for the latest Blog Post
		//function latestBlog(){
		//$blog = DataObject::get("BlogEntry", "", "Date DESC", "", 3);		
		//return $blog;	
		//}	
		
		function LatestBlogPosts($num=3) {
    	        return BlogPost::get()->sort('PublishDate','desc')->limit($num);
		}
		
		parent::init();
		// You can include any CSS or JS required by your project here.
		// See: http://doc.silverstripe.org/framework/en/reference/requirements
	}

Avatar
helenclarko

Community Member, 166 Posts

6 July 2015 at 11:52am

Hi Mhdesign,

Can you take the LatestBlogPosts function out of the init function?

Try the following:

<?php
class Page extends SiteTree {

	private static $db = array('SidebarHTML' => 'HTMLText'
	);

	private static $has_one = array(
	);
	
function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields -> addFieldToTab("Root.Sidebar",new HTMLEditorField("SidebarHTML"));
		return $fields;
	}
	
}

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 for the latest Blog Post
       function latestBlog(){
		$blog = DataObject::get("BlogEntry", "", "Date DESC", "", 3);		
		return $blog;	
	}	

If you are using <% loop latestBlog %> in HomePage.ss, you will need to use the latestBlog function in your page.php.

Avatar
mhdesign

Community Member, 216 Posts

6 July 2015 at 2:19pm

Nope. Still not working at all... same result after amending Page.php and checking template file.

Avatar
Pyromanik

Community Member, 419 Posts

8 July 2015 at 10:14am

Does HomePage_Controller extend Page_Controller?

Avatar
mhdesign

Community Member, 216 Posts

8 July 2015 at 1:57pm

Hi Pyromanic, yes it does. You can probably see from the attached code, however, that the PHP for this function is on Page.php, and the 'latest blog posts' panel appears on all site pages (including Homepage).

Avatar
Pyromanik

Community Member, 419 Posts

8 July 2015 at 8:57pm

Edited: 08/07/2015 9:04pm

What version of SilverStripe are you using?
HelenClarko keeps posting 3 years out of date code (that is also deprecated and should throw errors in more recent versions of SS), it's a bit confusing.

For 3.x you should use the following:

class Page_Controller extends ContentController {
	public function latestBlog($num=3) {
		return BlogPost::get()->sort('PublishDate','desc')->limit($num);
	}
}

And in the template:

<ol>
	<% loop $latestBlog %>
		<li>$Title</li>
	<% end_loop %>
</ol>