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.
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.
- Previous 10 entries
- 1
- Page 22(current)
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');
}
}
I just noticed. In page.php, the function is called LatestNews, but in Homepage.ss its called LatestBlogPosts. That would account for the failure.
Bruce, that fixed it. Thanks so much!
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.
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;
}
- Previous 10 entries
- 1
- Page 22(current)