Top Menu

How to Display Thumbnail from WordPress RSS feed using SimplePie?


I was designing home page for my theme showcase WordPress site Themeappears which is MultiSite enabled. The requirement was to display the recent post thumbnail from sub domains(Premium WordPress, Free WordPress and Free Blogger) in home page.

I decided to use SimplePie to syndicate my feeds. I searched the web and found Flickr Gallery with SimplePie but it was for flickr feed. I needed one which can read WordPress RSS feed and display the image from the post as thumbnail. So i decided to build with this sample for WordPress RSS feed.

Here is the final outcome. Take a look at the Live Demo and download the source code if you want.

Here is the main index.php page code where we are creating SimplePie object and defining RSS feed and functions for extracting thumbnail image link from the feed.

<?php
//Including SIMPLEPIE classes and functions
require_once('SIMPLEPIE_RSS_SHOWCASE/includes/simplepie.inc');
//Creating object of Simplepie i.e $feed
$feed = new Simplepie('http://themeappears.com/premiumwordpresstemplates/feed/rss/');
$feed->set_cache_duration(600);
$feed->init();
$feed->handle_content_type();


//This function will get an image from the feed

function returnImage ($text) {
    $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
    $pattern = "/<img[^>]+\>/i";
    preg_match($pattern, $text, $matches);
    $text = $matches[0];
    return $text;
}


//This function will filter out image url which we got from previous returnImage() function

function scrapeImage($text) {
    $pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/';     
    preg_match($pattern, $text, $link);
    $link = $link[1];
    $link = urldecode($link);
    return $link;

}


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $feed->get_title(); ?></title>
<link rel="stylesheet" type="text/css" href="SIMPLEPIE_RSS_SHOWCASE/style.css" />
<link rel="stylesheet" type="text/css" href="SIMPLEPIE_RSS_SHOWCASE/thickbox.css" />
<script type="text/javascript" src="SIMPLEPIE_RSS_SHOWCASE/jquery-1.3.1.min.js"></script>
<script type="text/javascript" src="SIMPLEPIE_RSS_SHOWCASE/thickbox-compressed.js"></script>
<script type="text/javascript" src="SIMPLEPIE_RSS_SHOWCASE/script.js"></script>
</head>

<body>
    <div class="page-wrapper">
        <div class="header">
            <h1><?php echo $feed->get_title(); ?></h1>
        </div>
        <div class="album-wrapper">
            <?php foreach ($feed->get_items() as $item): ?>
                <div class="photo">
                
               
               <?php $feedDescription = $item->get_description();
                    $image = returnImage($feedDescription);
                    $image = scrapeImage($image);
                    $image_url= $item->get_permalink();
                    $image_title= $item->get_title();
                    $post_date = $item->get_date();
//This will print the site title                    
echo '<a href="' . $image_url . '">'. $image_title.'</a>'."\n";
//This will display the thumbnail for each posts
echo '<a href="' . $image_url . '" title="' . $image_title . '"><img src="' . $image . '" /></a>'."\n";
?>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</body>
</html>

This SimplePie trick is for WordPress RSS feed. I didn’t try for Blogger and any other blog feeds. Try yourself if you need for any other feeds or put your comments, i will try to incorporate.

Note:

1.

The default cache location will be(./cache) in the same location where the PHP file with following code is there and folder should be chmod ’777′ i.e writable.

//Including SIMPLEPIE classes and functions
require_once('SIMPLEPIE_RSS_SHOWCASE/includes/simplepie.inc');

2.

SimplePie is already comes with WordPress installation. The functions can be used directly in pages without including ‘simplepie.inc’. I will give details instruction on how to use SimplePie in WordPress blog in next post.

3.

This script will display image thumbnails only when if the RSS feed has thumbnails in it. If your WordPress RSS don’t have thumbnails in it then try to add with this tutorial : How to Add Thumbnails to WordPress RSS Feed

Thanks to http://net.tutsplus.com

Also Read: 6 High Quality Premium WordPress Showcase Themes

Best Items - Start Your Own Community Website Like Facebook In a Minute

Want to start your own social or community website like Facebook ? Start right away using Buddypress WordPress plugin along with premium Buddypress Themes. No need to setup anything. It works out of box. Install BuddyPress plugin and Premium themes from this list. Your are done!.
FIND OUT MORE

Best Items - Top e-Commerce WordPress Plugins

Find 61+ top notch e-Commerce WordPress plugins - Paypal, Jigoshop, Amazon, WooCommerce, Credit Card Gateway and Authorize etc.
FIND OUT MORE

Best Items - Collection of Best Premium Portfolio WordPress themes

Need to showcase your project work in a stunning way? These premium Portfolio WordPress themes will make your work easy for you. Perfect for showcasing Project, Photography, Video and Art work.
FIND OUT MORE

Best Items - Top notch Premium BuddyPress Themes

Are you looking for best premium WordPress themes which are BuddyPress ? Head over to Premium BuddyPress Themes for top quality and responsive designs.
FIND OUT MORE

Best Items - Best Restaurant WordPress Theme

Collection of best Premium restaurant WordPress themes ready to be used for your and client projects.
FIND OUT MORE

Best Items - 200+ Popular HTML Email and Newsletter Templates for Effective Marketing

Do you want to market your products effectively ? There are 200+ great collection of E-mail and Newsletter HTML templates from Themeforest. Get into site directly and search for the suitable template that boost your sales and conversion rate.
FIND OUT MORE

Best Items - Best WordPress Templates Made for Mobile and Tablet Visitors

Collection of Premium WordPress templates to take your website to the next level. Use these themes on your project to reach out Mobile and tablet users. Grow your business and traffic instantly.
FIND OUT MORE

Best Items - Best Gallery WordPress Plugins

Find most downloaded WordPress Gallery plugins - Video, Thumbnail and Photo Gallery etc...
FIND OUT MORE

, , ,

  • http://szafiarka.com/ Szafiarka

    Hello, please take a look at that site… I am using the simplepie example that I found in the WP codex + I added the p> get_description(); ?>

    Can I show the thumbnails of the featured images together with the descriptions of each news?

    Thank you