Musings
Random RSS items with Magpie Expression Engine Plugin
18th December 2008
The Magpie RSS feed plugin for Expression Engine is ace, but there’s no support for displaying a random feed from the source. The following snippet shows you how.
{exp:magpie url="YOURFEED" limit="3" refresh="720" parse="inward"}
{items}
<?php
$feeds[] = array (
'title' => '<h3><a href="{link}">}{title}</a></h3>' ,
'feed' => '<p>{description}</p>' );
?>
{/items}
{/exp:magpie}
<?php $i = rand(0,count($feeds)-1); echo $feeds[$i]['title'].$feeds[$i]['feed']; ?>
It works by adding each item to a PHP array, then picking one out at random, you could easily adapt it to display several random articles too (comment if you’d like to see how.) Because we’re using php you will need to make sure that you go to preferences and set PHP to allowed and the parse stage to output.
As a side note the reason I’ve add the
parse="inward"
tag is because then you can happily use word limiting on the items in the feed.
Discuss
Get involved in the discussion by using the comment form below.
4th January 2009
@Ron If you want to output ALL magpie RSS feeds in a random order you could ditch the bit picking a random item from the array ($i) and just shuffle it then do a foreach:
shuffle ($feeds);
foreach ($feeds as $feed) :
echo $feed[‘title’].$feed[‘feed’];
endforeach;
Hope that helps.
3rd February 2009
Took the freedom to copy your code in EE
Feed displays but it adds some characters
like ’ ); ?> ‘
Don’t know why??
3rd February 2009
@anton I’d suggest you check that the single quote marks in the array definition are definitely right - it’s probably including the php as part of the string. Just go through the script and replace all the single quote marks to ensure they’re all correct.
4th February 2009
checked code as recommended, did not change anything, I have to digg deeper to find the problem, thank you for advise.
4th February 2009
@anton pop your code in http://pastie.org/ and I’ll take a look if you want.
Ron
4th January 2009
I need it to display all 5 feeds, each consisting of one post which is all there is, so I don’t need to set limit.
I have rand(0,count($feeds)-1) and tried playing with these numbers, but no luck.