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

Pulling SS Blog from another pages


Go to End


14 Posts   5874 Views

Avatar
Bruce B

Community Member, 164 Posts

3 July 2011 at 11:19am

Bstarr,
In page.php, you need to move the LatestNews function. It should sit in the page class near the top of the code. Look for a single closing bracket } around line 10 and insert it just above that.

Avatar
Bstarr

Community Member, 25 Posts

3 July 2011 at 3:57pm

I moved it but sadly I'm still only seeing the no entries message. Any other suggestions?

<?php
class Page extends SiteTree {

	public static $db = array(
	);

	public static $has_one = array(
	);

function LatestNews($num=4) {
$news = DataObject::get_one("BlogHolder");
return ($news) ? DataObject::get("BlogEntry", "ParentID = $news->ID", "Date DESC", "", $num) : false;
}
}
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
	 */
	public static $allowed_actions = array (
	);

	public function init() {
		parent::init();

		// 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::themedCSS('layout'); 
		Requirements::themedCSS('typography'); 
		Requirements::themedCSS('form'); 
	}
}

Avatar
Bruce B

Community Member, 164 Posts

3 July 2011 at 4:11pm

I just noticed. In page.php, the function is called LatestNews, but in Homepage.ss its called LatestBlogPosts. That would account for the failure.

Avatar
Bstarr

Community Member, 25 Posts

3 July 2011 at 4:24pm

Bruce, that fixed it. Thanks so much!

Avatar
scpi

Community Member, 21 Posts

23 August 2011 at 7:08am

Along these lines, what would be the best way to extend this code to pull only blog posts with a specific tag? For example, to just pull posts tagged with "news" into a list on an "about us" page?

I see a ShowTag() function in BlogHolder.php, but that's looking for URL parameters. SelectedTag() in BlogTree.php appears to do the same thing. Entries() in BlogTree.php looks promising, but I'm not sure I can get there from here. Everything else I'm coming up with is pretty convoluted. I feel like I'm missing something, and this ought to be easier than I'm making it.

Avatar
scpi

Community Member, 21 Posts

23 August 2011 at 7:25am

Edited: 23/08/2011 7:26am

To follow up, I can make this work via SQL, borrowing some code from Entries() in BlogTree.php. But I feel dirty when I copy & paste code like this.

function LatestBlogPosts($num=5, $tag='') { 
   if($tag) {
      $SQL_tag = Convert::raw2sql($tag);
      $tagCheck = "AND \"BlogEntry\".\"Tags\" LIKE '%$SQL_tag%'";
   } else {
      $tagCheck = '';
   }
   $blogs = DataObject::get_one("BlogHolder"); 
   return ($blogs) ? DataObject::get("BlogEntry", "ParentID = $blogs->ID ".$tagCheck, "Date DESC", "", $num) : false; 
}

Go to Top