Filtering out RSS items with Magpie that contain a specific image

398 views Asked by At

Working with Wordpress on this. I'm using Magpie to grab an RSS feed, but I only want it to display items that include a specific image. Here is the particular tag that I want to search for:

<media:thumbnail url="http://media.publicbroadcasting.net/wfpl/events/images/ourPick5.gif"/>

And here's the specific code I'm using in this application. It's basic, and I'm not incredibly adept at Magpie or PHP (I'm assuming there are more elegant ways of doing this for someone with a more robust knowledge of PHP):

<?php include_once(ABSPATH.WPINC.'/rss.php'); $feed = fetch_rss('http://www.publicbroadcasting.net/wfpl/.eventsfeed'); $items = array_slice($feed->items, 0, 4);?>

<?php if (!empty($items)) : ?>
<?php foreach ($items as $item) : ?>

<?php if (!preg_match('/<media:thumbnail\surl\="http:\/\/media.publicbroadcasting.net\/wfpl\/events\/images\/ourPick5.gif".+?>/i', $item['description'])) 
continue; ?>

    <li><a href="<?php echo $item['link']; ?>"><div class="item-headline"><?php echo $item['title']; ?></div>
    <div class="item-info"><?php echo $item['description']; ?>
    </div></a></li></ul>

<?php endforeach; ?>
<?php endif; ?>

Again, I only want to display the items with that specific image url in the "media:thumbnail" tag and discard all other items.

1

There are 1 answers

10
Teno On BEST ANSWER

Add this part,

<?php if (!preg_match('/<media:thumbnail\surl\="http:\/\/media.publicbroadcasting.net\/wfpl\/events\/images\/ourPick5.gif".+?>/i', $item['description'])) 
    continue; ?>

before

<li><a href="<?php echo $item['link']; ?>"><div class="item-headline"><?php echo $item['title']; ?></div>
    <div class="item-info"><?php echo $item['description']; ?>
    </div></a></li>