SimplePie Multiple Feeds Random Order

2019-08-02 10:04发布

问题:

I am new to SimplePie and was looking through the API and couldn't find what I needed. I need to use Simple Pie to get random articles from multiple feeds. I'll better explain my question:

Here's how SimplePie gives me the feed now (in chronological order):

  • Article 1 (CNN)
  • Article 2 (CNN)
  • Article 3 (CNN)
  • Article 4 (CNN)
  • Article 5 (CNN)

  • Article 1 (YAHOO)

  • Article 2 (YAHOO)
  • Article 3 (YAHOO)
  • Article 4 (YAHOO)
  • Article 5 (YAHOO)

  • Article 1 (GOOGLE)

  • Article 2 (GOOGLE)
  • Article 3 (GOOGLE)
  • Article 4 (GOOGLE)
  • Article 5 (GOOGLE)

Here is how I need it (in random order):

  • Article 3 (CNN)
  • Article 1 (GOOGLE)
  • Article 5 (YAHOO)
  • Article 3 (GOOGLE)
  • Article 2 (CNN)
  • Article 4 (YAHOO)
  • Article 1 (CNN)
  • Article 3 (GOOGLE)
  • Article 3 (YAHOO)

and so forth..

As far as I know, I would have to make a separate SimplePie feed everytime just to pull one unique article. I want Simple Pie to be able to do this with one call, but just rearranging them in before it sends it back. So kind of like a custom resort.

Please help :)

回答1:

I had the same question here: Custom Sort Order with SimplePie

You need to extend the class.

class SimplePie_Random_Sort extends SimplePie
{
    public static function sort_items($a, $b)
    {
        return rand(-1, 1);
    }
}