Jump to:

1851 Posts in 1606 Topics by 561 members

Blog Module

SilverStripe Forums » Blog Module » [SOLVED]Pull latest blog post except the one that is currently viewed. Please Help.

Discuss the Blog Module.

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

Page: 1 2 3
Go to End
Author Topic: 2197 Views
  • Juan
    Avatar
    Community Member
    18 Posts

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

    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

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: [SOLVED]Pull latest blog post except the one that is currently viewed. Please Help. Link to this post

    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
    ));

  • Juan
    Avatar
    Community Member
    18 Posts

    Re: [SOLVED]Pull latest blog post except the one that is currently viewed. Please Help. Link to this post

    hello thanks for the reply.

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

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: [SOLVED]Pull latest blog post except the one that is currently viewed. Please Help. Link to this post

    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..

  • Juan
    Avatar
    Community Member
    18 Posts

    Re: [SOLVED]Pull latest blog post except the one that is currently viewed. Please Help. Link to this post

    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;
    }

  • Juan
    Avatar
    Community Member
    18 Posts
  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: [SOLVED]Pull latest blog post except the one that is currently viewed. Please Help. Link to this post

    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.

  • Juan
    Avatar
    Community Member
    18 Posts

    Re: [SOLVED]Pull latest blog post except the one that is currently viewed. Please Help. Link to this post

    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);

    2197 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.