Jump to:

23490 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » [FIXED] "Previous or Next Buttons" example not fully working

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1643 Views
  • Johan
    Avatar
    Community Member
    49 Posts

    [FIXED] "Previous or Next Buttons" example not fully working Link to this post

    Using the exact same code as on: http://doc.silverstripe.org/recipes:previousornext

    The example on here worked beautifully for cycling through my News pages in the news section.

    However when I use the same page template (ArticlePage.cc/php) again for the Publication and Events section the previous or next buttons fail to work. This is my site structure...

    Seems odd when i have used exactly the same code as the example and same page template (ArticlePage.cc/php)

  • TotalNet
    Avatar
    Community Member
    181 Posts

    Re: [FIXED] "Previous or Next Buttons" example not fully working Link to this post

    Hi Johan,

    What do you mean by "fail to work"? Do the links show at all and if so where do they link to?

    Have you put the code in your Page_Controller or ArticlePage_Controller?

    This is a standard addition to my Page.php and using it without issue so I'm sure we can figure this out.

  • SSadmin
    Avatar
    Community Member
    90 Posts

    Re: [FIXED] "Previous or Next Buttons" example not fully working Link to this post

    i have got this function works quite well for me?!
    not sure why it fails on yours.
    Maybe provide some code of your (ArticlePage.cc/php) and template .ss could be helpful.

  • Johan
    Avatar
    Community Member
    49 Posts

    Re: [FIXED] "Previous or Next Buttons" example not fully working Link to this post

    1. Only in the news pages do they work (using ArticlePage).

    2. Publications and Events the paging does not show at all (again using ArticlePage).

    Code ArticlePage.php:

    <?php
    /**
    * Defines the ArticlePage page type
    */
    class ArticlePage extends Page {
    static $db = array(
          'Date' => 'Date',
          'Author' => 'Text',   
    );
    static $has_one = array(
    );
    static $icon = "themes/tutorial/images/treeicons/news";

       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $fields->addFieldToTab('Root.Content.Main', new TextField('Author'), 'Content');
          // $fields->addFieldToTab('Root.Content.Main', new DateField('Date'), 'Content');
          
          $datefield = new DateField('Date');
          $datefield->setConfig('showcalendar', true);
          $datefield->setConfig('showdropdown', true);
          $datefield->setConfig('dateformat', 'dd/MM/YYYY');
          
          $fields->addFieldToTab('Root.Content.Main', $datefield, 'Content');

          return $fields;
       }

    }

    class ArticlePage_Controller extends Page_Controller {

       function LatestNews($num=3) {
          $news = DataObject::get_one("NewsHolder");
          return ($news) ? DataObject::get("ArticlePage", "ParentID = $news->ID", "Date DESC", "", $num) : false;
       }

    // ************ Previous or Next Buttons *************
       
    public function nextPager() {
    $where = "ParentID = ($this->ParentID + 0) AND Sort > ($this->Sort + 0 )";
    $pages = DataObject::get("SiteTree", $where, "Sort", "", 1);
    if($pages) {
    foreach($pages as $page) {
    return $page;
    }
    }
    }
    public function previousPager() {
    $where = "ParentID = ($this->ParentID + 0) AND Sort < ($this->Sort + 0)";
    $pages = DataObject::get("SiteTree", $where, "Sort DESC", "", 1);
    if($pages) {
    foreach($pages as $page) {
    return $page;
    }
    }
    }

    }

    ?>

    ArticlePage.ss

    <div class="paging">
        <% if previousPager %>
        <p class="previous"><a href="$previousPager.URLSegment">Previous</a></p>
        <% end_if %>
        <% if nextPager %>
        <p class="next"><a href="$nextPager.URLSegment">Next</a></p>
        <% end_if %>
    </div>

    Many thanks.

  • Johan
    Avatar
    Community Member
    49 Posts

    Re: [FIXED] "Previous or Next Buttons" example not fully working Link to this post

    A programmer on IRC has found the issue.

    The reason it fails is because all the articles have the same date.

    Changed the dates now work.

    1643 Views
Page: 1
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.