How to display an RSS feed on your site or wordpress blog? How to display rss feed in HTML using PHP? Some times you may want to display the RSS feed of a popular news blog on your site. If you maintain multiple websites or blogs you can display the RSS feed of one site on another site. This will improve your sites visibility and page views.
To display an RSS feed and its content you need to create a Page and use the page to display the RSS Feed or you need to embed the PHP code in post or page or widget. As we discussed in earlier post ,usually wordpress won’t allow you to include PHP code in pots, page or sidebar. Read how to Insert, Execute, Embed or Include PHP Code in WordPress Post, Page or Sidebar.


Related:

There are two ways to display the RSS feed details on your blog page. You can either use the WordPress Feed API to fetch and parse the RSS feed or cURL and SimpleXML to retrieve and parse the RSS feed.

Display any External RSS Feed on Your Site using WordPress Feed API

You can display the RSS feed on any page. If you want to display on home page, you need to edit the home page template and add the code given below.
To display the RSS feed and content on a new page, you need to create a php file (“ExternalRSS.php”). (Read: How to create a custom page in wordpress blog) After creating the file add the below given code in that file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<div style = 'display:block;width:500px;height:450px;border: 3px coral solid;'>
<?php 
include_once(ABSPATH.WPINC.'/feed.php');
 
$rss = fetch_feed('http://feeds.feedburner.com/globinch');
$displayitems = $rss->get_item_quantity(5);
$rss_items = $rss->get_items(0, $displayitems);
?>
<ul>
<?php if ($displayitems == 0) 
echo '<li>No items.</li>';
else
foreach ( $rss_items as $item ) : ?>
<li>
<a href='<?php echo $item->get_permalink(); ?>'
title='<?php echo $item->get_title(); ?>'>
<b><?php echo $item->get_title(); ?></b></a> <br/>
<?php echo 'Published On '.$item->get_date('j F Y | g:i a'); ?>
<br/>
<?php echo substr($item->get_description(), 0, 160) . '...'; ?>
</li>
<?php endforeach; ?>
</ul>
</div>

In the above code replace “globinch” with your – feedburner id – if you are using a feedburner feed. Elase replace the complete URL with your RSS Feed URL.
The above code will be displayed as below.

Display any External RSS Feed on Your Site Using PHP cURL and SimpleXML

PHP cURL is an API (libcurl) allows you to connect and communicate to many different types of servers with many different types of protocols. We can utilize the cURL and SimpleXML to display any RSS feed on your blog page. SimpleXML extension provides easy toolset to convert XML to an object that can be processed with normal property selectors and array iterators.
As mentioned above you can display the RSS feed on any page. If you want to display on home page or any other page, you need to edit the page template and add the code given below.
To display the RSS feed and content on a new page, you need to create a php file as mentioned above. (Read: How to Use WordPress Custom Page as Home Page) After creating the file add the below given code in that file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<div style = 'display:block;width:500px;height:450px;border: 3px coral solid;'>
<ul>
<?php 
	 $feedId = "globinch";
	 $stringVal="";
	 $url="http://feeds.feedburner.com/";  
	 $url= $url. $feedId;
	 $ch = curl_init();  
	 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
	 curl_setopt($ch, CURLOPT_URL, $url);  
	 curl_setopt($ch, CURLOPT_HEADER, 0);
	 $data = curl_exec($ch);  
	 curl_close($ch);  
	 $xml = new SimpleXMLElement($data,LIBXML_NOCDATA);
	 echo "<strong><CENTER><h2>".$xml->channel->title. "</h2></h3>" . $xml->channel->description. "</h3></CENTER></strong>";
     $cnt = 5;
	 //This is for all items : count($xml->channel->item);
   	 for($i=0; $i<$cnt; $i++)
     {
	    $url 	= $xml->channel->item[$i]->link;
	    $title 	= $xml->channel->item[$i]->title;
		$date 	= $xml->channel->item[$i]->pubDate;
	    $desc = substr($xml->channel->item[$i]->description, 0, 160) . '...';
 	    echo '<li><a href="'.$url.'">'.$title.'</a><br/>Published On: '.$date. '<br/>'.$desc.'</li>';
    }
	 ?>
</div>

This will be displayed as below.

, the author of Globinch.com, is a technology blogger and software architect. You can follow him on Twitter, Google+, Facebook. If you enjoyed reading the above article please consider sharing it.
Related Posts with Thumbnails
  • http://iraqi-dinarnews.com/ bhupen

    Your code is superb but the thing is after fetching all data from other site its not opening in my site instead of that its title redirecting the other site. I actually want when user click on title it will display full post in my site not redirect on other site. If there is a solution please provide me. Thanks in advance

    • globinch

      Hi Bhupen,

      It is possible, you need to edit the line no:24 where we add the anchor tag. You need to add the target attribute to the anchor tag. The target should be "_self".

      Btw.. Many thanks for visiting my site and for the comments

      Binu George

SEO Powered by Platinum SEO from Techblissonline

Switch to our mobile site