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.

Blog Module

Discuss the Blog Module.

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

Returning a Child Blog Page's Posts


Reply

2 Posts   820 Views

Avatar
reececropley

10 January 2013 at 12:37am Community Member, 11 Posts

Hello I have a site with multiple blogs on it.

The site is for a sports club and each sport has its own page (page.ss). These pages have children on Calenders, Blogs and other pages.

On the sport page I would like to display a feed of their specific blog.

I have managed to display a widget for there specific widget using the following code.

Page.php

public function GetChildren($Limit = 1){

      $Children = $this->Children();

      return $Children->limit($Limit, 0);

   }
   
   public function CalendarWidget() {
      return Calendar::get()->first()->CalendarWidget();
   }

Page.ss

<% control Children %>

   <% control $GetChildren %>
      $CalendarWidget
<% end_control %>
   

<% end_control %>

I was wondering if there is a way of displaying the blog feed for just the children.

I was thinking something along the lines of the following: (error code btw)

Page.php

public function LatestBlogEntry($num=4) {
      $Children = $this->Children();
      $blogpost = $Children::get_one("BlogHolder");
      return ($blogpost) ? DataObject::get("BlogEntry", "", "Date DESC", "", $num) : false;
   }

Has anyone done something similar before?

Avatar
IOTI

25 January 2013 at 6:03am (Last edited: 25 January 2013 6:05am), Community Member, 189 Posts

In this situation I wouldn't rely on using the children to get the blog holder. What if there are two blog holders? what if there are none? What i usually do in this situation is add a TreeDropdownField that allows a CMS user to pick the blog holder they want to use.

<?php

class Page extends SiteTree {

public static $has_one=array(
'BlogHolder'=>'SiteTree'
);

public function getCMSFields(){

$fields->addFieldToTab('Root.Content.Main', new TreeDropdownField('BlogHolderID','Select a Blog Holder', 'SiteTree'));
}

public function getBlogEntries($limit=4){
if($this->BlogHolderID){
if($blogHolder = DataObject::get_one('BlogHolder', $this->BlogHolderID)){
return $blogHolder->Entries($limit);
}else {
return false;
}
}else {
return false;
}
}

}

then on your template use <% control BlogEntries %>.