Musings

Random RSS items with Magpie Expression Engine Plugin

18th December 2008

Random RSS items with Magpie Expression Engine Plugin

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>' );
  ?&gt;
{/items}
{/exp:magpie}
&lt;?php $i = rand(0,count($feeds)-1); echo $feeds[$i]['title'].$feeds[$i]['feed']; ?&gt;

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.

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.

wil.linssen

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.

Karthikeyan

26th January 2009

Thanks wil.linssen really helped me lot smile

anton

3rd February 2009

Took the freedom to copy your code in EE
Feed displays but it adds some characters
like ’ ); ?> ‘
Don’t know why??

wil.linssen

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.

anton

4th February 2009

checked code as recommended, did not change anything, I have to digg deeper to find the problem, thank you for advise.

wil.linssen

4th February 2009

@anton pop your code in http://pastie.org/ and I’ll take a look if you want.

anton

4th February 2009

thank
already posted
by the way love that pastie

Leave a comment

Your email is never ever shared, and required fields are marked with *

*

*

Used for Gravatars



Sorry folks, uses the "nofollow" attribute

*


My apologies for this nonsense: please type the word you see below.



Copyright © 2009 Wil Linssen all rights reserved

Wil-Linssen.com is powered by ExpressionEngine: it's good, go and get it.