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

[SOLVED]Pull latest blog post except the one that is currently viewed. Please Help.


Go to End
Reply


18 Posts   2487 Views

Avatar
Juan

Community Member, 18 Posts

8 February 2013 at 5:14pm

Hello everyone,

i am newbie

i already have a function that can pull the latest post
but how do i pull the Latest Blog Post except the one that is currently viewed,

function getLatestPostsBySport() {
$sportHolder = $this->owner->Parent();
while($sportHolder && ($sportHolder->ClassName != 'RedirectorPage')) {
$sportHolder = $sportHolder->Parent();
}
if(!$sportHolder) return false;
return FeedAggregator::posts_by_sport($sportHolder,10);
}

please help.. thanks in advance

Avatar
Willr

Forum Moderator, 5513 Posts

9 February 2013 at 3:16pm

Well I'd imagine FeedAggregator::posts_by_sport() function has the code that is actually useful for this problem. If you're using 3.0 then you could just use the exclude() method in the ORM to exclude records by ID

$feed = BlogPost::get()->exclude(array(
'ID' => $this->ID
));

Avatar
Juan

Community Member, 18 Posts

10 February 2013 at 2:36pm

hello thanks for the reply.

No. i am using 2.4 i just updating an old website.. where should i put that code? :(

Avatar
Willr

Forum Moderator, 5513 Posts

10 February 2013 at 4:53pm

FeedAggregator::posts_by_sport() is the function you want to edit, since you haven't posted it, I can only help you so much.

You'll need to alter that function to pass the current page ID (i.e FeedAggregator::posts_by_sport($sportHolder,10, $this->ID); ). Find that function you've created and it should have a DataObject::get() call, You'll need to add another statement to ignore the page ID..

Avatar
Juan

Community Member, 18 Posts

10 February 2013 at 7:56pm

Edited: 10/02/2013 7:56pm

hello willr,

have found it.. what should i add to DataObject::get

Thanks.. :)

static function posts_by_sport($sport,$limit = 20) {
if(is_string($sport)) {
$sectionPage = DataObject::get_one('RedirectorPage',"URLSegment = '$sport'");//consider changing to search all SiteTree, if structure changes
} else {
$sectionPage = $sport;
}
if(!$sectionPage) return false;
$firstChildren = $sectionPage->AllChildren();
$idArr = $firstChildren->column('ID');
$fs = $firstChildren->getIterator();
foreach($fs as $f) {
if(Director::isDev()) {
//print 'Getting children for "'.$f->Title.'"<br />'.PHP_EOL;
//print_r($f->AllChildren()->column('ID'));
}
$idArr = array_merge($idArr,$f->AllChildren()->column('ID'));
}
if(Director::isDev()) {
//print_r($idArr);
}
$idList = implode(',',$idArr);
$posts = DataObject::get('BlogEntry','ParentID IN('.$idList.')','Date DESC','',$limit);
return $posts ? $posts : false;
}

Avatar
Juan

Community Member, 18 Posts

12 February 2013 at 8:40pm

Helloo? :S

Avatar
Willr

Forum Moderator, 5513 Posts

12 February 2013 at 8:44pm

If you want real time support try the IRC channel, always developers wanting to help there..

So.. what you should do

1) Change static function posts_by_sport($sport,$limit = 20) { to static function posts_by_sport($sport,$limit = 20, $postID = null) {

2) Change that the line $posts = DataObject::get('BlogEntry','ParentID IN('.$idList.')','Date DESC','',$limit); to

if($postID) {
$posts = DataObject::get('BlogEntry','ParentID IN('.$idList.') AND ID != '. (int) $postID,'Date DESC','',$limit);
}
else {
$posts = DataObject::get('BlogEntry','ParentID IN('.$idList.')','Date DESC','',$limit);
}

3) Change your call to that function (return FeedAggregator::posts_by_sport($sportHolder,10); ) to return FeedAggregator::posts_by_sport($sportHolder,10, $this->ID);

And there you go! I suggest reading some PHP tutorials to get your head around those changes if they don't make sense.

Avatar
Juan

Community Member, 18 Posts

12 February 2013 at 9:20pm

Edited: 12/02/2013 9:21pm

thanks for the reply willr

dont worry i know how to apply those changes..

but i got an error..

Undefined property: BlogHolderDecorator::$ID

in this line

return FeedAggregator::posts_by_sport($sportHolder,10, $this->ID);

Go to Top