Jump to:

1851 Posts in 1606 Topics by 561 members

Blog Module

SilverStripe Forums » Blog Module » Latest articles first in Blog & Articles

Discuss the Blog Module.

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

Page: 1
Go to End
Author Topic: 1009 Views
  • mrjohnson
    Avatar
    Community Member
    4 Posts

    Latest articles first in Blog & Articles Link to this post

    Hello

    I am having a problem blog articles and regular articles to show the latest article first.

    It is showing the last first and latest at the bottom.

    Can someone please advise what code I need to change to show the latest first.

    Thanks

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Latest articles first in Blog & Articles Link to this post

    How are you showing the list of articles. You can pass the sort as the 3rd argument to DataObject

    $articles = DataObject::get('BlogArtice', "", "Date DESC");

  • mrjohnson
    Avatar
    Community Member
    4 Posts

    Re: Latest articles first in Blog & Articles Link to this post

    I am editing the work of someone else so not very familiar with the system. I cannot find anything similar to the code above.

    There is nothing like it in /mysite/code/BlogPage.php or BlogHolder.php

    I can post code from a specific file if that would help

  • mrjohnson
    Avatar
    Community Member
    4 Posts

    Re: Latest articles first in Blog & Articles Link to this post

    If someone can help with this I would be greatly appreciative.

    I am happy to pay someone for support.

  • boombox
    Avatar
    Community Member
    44 Posts

    Re: Latest articles first in Blog & Articles Link to this post

    Hi Willr
    I am using the RSS Widget to return an external RSS Feed - see code below
    This is working fine except items bring back the latest articles at the top of the list - I want the oldest (upcoming) articles at the top of the list for the sourced feed items.

    ### Code in RSSWidget.php ###

    line 77
    ...
    if($items = $feed->get_items(0, $this->NumberOfItems, $this->NumberToShow)) {
             foreach($items as $item) {
                
                // Cast the Date
                $date = new Date('Date');
                $date->setValue($item->get_date());

                // Cast the Title
                $title = new Text('Title');
                $title->setValue($item->get_title());

                // Cast the Description
                $description = new Text('Description');
                $description->setValue($item->get_description());

                // store the item in our array we set up.
                $output->push(new ArrayData(array(
                   'Title' => $title,
                   'Date' => $date,
                   'Description' => $description,
                   'Link' => $item->get_link()
                )));

                $output -> sort("Date", "ASC");
                
             }
             return $output;

          }

    ####

    This is returning the number of items (set in widget config) to 5 items
    But the actual number of items in the feed is approx 50 so even if I change the code to $output -> sort("Date", "DESC"); it just changes the order of the 5 items presented and not the order of the toatl 50 items in the RSS feed

    Whats the best way to approach this?
    I tried
    - returning the number of items (set in widget config) to 50 items
    - limiting in the template the number of items displayed (hack) but this doesn't work

    e.g. <% control FeedItems(5) %>

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Latest articles first in Blog & Articles Link to this post

    @boombox - most rss feeds return the latest at the top, the only way you can get the oldest is to set the number to get to max length of the rss feed (say 50) then do the sort as you have.

  • boombox
    Avatar
    Community Member
    44 Posts

    Re: Latest articles first in Blog & Articles Link to this post

    @Willr - yes thanks have tried that and getting the order of the latest first
    - however how do I limit the template output to say only the latest 5 results?
    I have tried limiting the RSSWidget.ss to the following set but this is not limiting the result?

    <% control FeedItems(5) %>

    Cheers
    Boombox

  • boombox
    Avatar
    Community Member
    44 Posts

    Re: Latest articles first in Blog & Articles Link to this post

    <SOLVED>
    I have solved how to limit the number of items being displayed from a feed using the RSS widget

    In RSSWidget.php (attached) I have
    - added in a NumberofItems variable - this is used to set the number of items aggregated from the feed source
    - used the NumberToShow variable to set how many is actually output on the template

    So by aggregating all the feed items (or max number, then ordering by ascending to display the upcoming ones at top of the feed output and then controlling the actual number display in the template I can now have the upcoming items by date displayed first) - useful for event based rss feeds

    In the function FeedItems I have added the following code from line 98

    ...
    $output -> sort("Date", "ASC");
    $limitoutput = $output->getRange(0, $this->NumberToShow);
    }
    return $limitoutput;   

    Happy Feeding

    Attached Files
    1009 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.