Import RSS feeds into your WordPress blog

It's actually very easy to import RSS Feeds and display them on your WordPress Site, e.g. in the sidebar. WordPress, as well as PHP, provides functions to fetch the feed, parse it and provide it to the user itself.

The opportunities are versatile, for example you can import your recent flickr photos or display the latest articles from partner sites and so on. I provide an example of importing your own RSS feed, for example to display it on your front page instead of the loop. This has the benefit of querying the database only one time, as oppose to the multiple times it would be executed via the loop.


<?php get_header(); ?>

<?php require_once (ABSPATH . WPINC . '/rss-functions.php'); ?>
<?php $today = current_time('mysql', 1); ?>

The line should be familiar, it imports the header of the current theme. Then we include rss-functions.php from WordPress to get the RSS functionality. Also, we get the current date for later reference.


<div class="main">
  <h2>Recent Postings</h2>

<?php
$rss = @fetch_rss('http://www.jeriko.de/feed/');
if ( isset($rss->items) && 0 != count($rss->items) ) {

Nothing special at first, just a simple header to make clear what we're showing. Then we get the RSS feed via @fetch_rss from the given URL. A simple check will make clear if the feed actually has entries.


<ol>
<?php
$rss->items = array_slice($rss->items, 0, 5);
foreach ($rss->items as $item ) {
?>
<li>
  <a href='<?php echo wp_filter_kses($item['link']); ?>'>
  <?php echo wp_specialchars($item['title']); ?>
  <small>—
    <?php echo human_time_diff( strtotime($item[’pubdate’], time() ) ); ?>
    <?php _e(’ago’); ?>
  </small>
</li>

Now here comes the fun part, we split the feed into the entries and will get the five newest ones. If you want more, change the appropriate parameter in array_slice. Then we will output each of the entries via foreach. Everything inside this loop are just special formattings of the specific parts of an entry, it's up to you how you want them to be displayed.


<?php
}
}
?>
</ol>

This closes every loop as well as the unordered list. And thats pretty much it, it will display the titles and dates of the last five entries.

Below is the structure for an RSS feed provided by WordPress:

  • title (the title of the posting)
  • link (the permalink of the posting)
  • pubDate (the date on which the posting was made)
  • dc:Creator (the author of the posting)
  • description (what would be shown as the excerpt of the posting, either the beginning of the posting or the excerpt itself)
  • content:encoded (the content of the posting)

If you use the code above, you access this fields via $item['fieldname'].

One thing to remember: If you use RSS feeds from other sites than your own, make sure that you either (1) clearly mark them as external entries and not your own and/or (2) ask the original author of the feed for permission to use them on your site.

 
Tweet This
1 Votes 1 Star
Ähnliche Beiträge

3 Trackbacks

  1. 02.06.2007 - Redesign - Part 1 at dekay.org
  2. 12.03.2008 - estetika » Blog Archive » links for 2008-03-12 | portfolio of intera [...]
  3. 13.06.2009 - RR [dot] net / How to publish RSS feed into your blog site

Diskussion: 14 Kommentare

  1. cargawar (Website)

    Just a tip for webmasters:
    I use R-mail (http://r-mail.org) to get notified on new comments on my site…
    I don’t have the time to have an RSS-reader open all the time and I’m mostly interested in these comments (I used to have some dorks spamming my site)…this is how I keep it clean

    18. April 2006 um 10:39 Uhr - AntwortenReply to this comment
  2. Marc Holt

    I am completely new to this RSS technology. Your description of ‘how simple’ it is to install doesn’t tell me anything I can use.

    WHERE DO I PUT THE CODE?
    How about some samples, please?

    20. Mai 2007 um 09:41 Uhr - AntwortenReply to this comment
  3. IntelligentProgrammerPerson (Website)

    Wow, Marc Holt, you are such a dick.

    Why don’t you learn a little about computers instead of just exploiting them. Information will serve you for the rest of your life, being mean to someone who is giving you something free will not.

    07. Juni 2007 um 19:08 Uhr - AntwortenReply to this comment
  4. Marc Holt (Website)

    Jeriko One, so I see that intelligent guidance and intelligent comment are both severely lacking on this site. Free or not, if someone is going to ‘explain’ how to do something they should start at the beginning…not assume readers are as ‘intelligent’ as them.

    Your stupid comment just shows the level of maturity and intelligence here….NOT!

    08. Juni 2007 um 01:19 Uhr - AntwortenReply to this comment
  5. Jeriko (Website)

    Maybe you should take a look at other posts instead of judging by one comment, which even isn’t made by me. I’m not the one to censor anything around here, if “IntelligentProgrammerPerson” wants to speak up, why not.

    Aside from that, maybe you should take a look at the WordPress Codex, almost everything related to WordPress is explained there, including. the template hierarchy, which will briefly explain, which file is responsible for what.

    Oh and just a quick note, I don’t give a damn if people say I’m an asshole. But maybe you should watch your language on other places, others than me might be offended. But you do know that as an intelligent person, right? ;-)

    08. Juni 2007 um 10:47 Uhr - AntwortenReply to this comment
  6. M (Website)

    Jeriko, my apologies. That comment should have been directed at the IntelligentProgrammerPerson instead for his childish outburst.

    You all may be interested to know that I have been in the computer business for more than 30 years, starting out on punched cards before any of you were born. I run my own IT company and have done so for many years. So I am not exactly a newcomer.

    Why couldn’t I understand how to implement this technology? Because the instructions given made some basic assumptions that didn’t take into account someone new to this technology. As I pointed out, this forum tells us HOW to write the code, but not WHERE to implement it. Do we put it in the PHP code, or do we add it to the page template? Where and how exactly do we implement it? This is the information that would make this page worthwhile.

    12. Juni 2007 um 00:36 Uhr - AntwortenReply to this comment
  7. Tanvir @ BareFly.com (Website)

    Thanks for the nice tip, how do I make it work on wordpress.com subdomain free blogs I wonder?

    03. März 2008 um 11:54 Uhr - AntwortenReply to this comment
  8. Monkeygames.ws (Website)

    Your website is a great

    thanks

    31. März 2008 um 12:13 Uhr - AntwortenReply to this comment
  9. Liam

    Hi,

    i am currently developing a website and trying to stream an external RSS and display it on a page in the website.

    i am fairly new to php and web design, so still learning the ropes. I am currently having trouble with some syntax you have used “->” in youe ‘fun’ section =]
    i.e.:
    foreach ($rss->items as $item ) {

    i keep getting error messages saying ‘unexpected syntax’ :

    Parse error: syntax error, unexpected ‘.’, expecting ‘,’ or ‘)’ in C:wampwwwdark_dimensionindex.php on line 22

    just wondering if you could shed some light on this

    Thanks
    Liam

    28. April 2008 um 10:26 Uhr - AntwortenReply to this comment
  10. Catherine (Website)

    Hi Jeriko,

    I think this is just what I’ve been looking for. :-)

    I’m kind of new to this, so I need some advice. In addition to pulling in a feed from one of my sites into another, I actually would love for it to also include a thumbnail on the feed. The posts on the sites I’m importing include a thumbnail created in the custom-field under the key name “thumbnail”.

    What would you add to your script to call this custom field into the rss feed?

    Your advice would be greatly appreciated! :-)

    02. Juni 2008 um 20:15 Uhr - AntwortenReply to this comment
  11. Jensen (Website)

    Hello, Thanks for your tutorial. I wonder if there is a way i can have RSS as a post? Is it possible? I would like to autopost with RSS. Thanks.

    07. August 2008 um 16:55 Uhr - AntwortenReply to this comment
  12. Dan

    Good tutorial. That Marc guy is an idiot. The answer on where to implement it – Wherever the hell you want to pull an RSS feed.

    If you’re a 30 year vet it’s no wonder you’re being replaced by 20 yr old kids that learn your entire lifetime of knowledge in a matter of years.

    28. April 2009 um 09:33 Uhr - AntwortenReply to this comment
  13. dcannon1 (Website)

    One small issue with the above code. You need to close your link, so drop a closing ‘a’ tag after the closing small tag and it should be good to go!

    28. April 2009 um 09:39 Uhr - AntwortenReply to this comment
  14. Inspirator

    hi, Jerico)
    tx for simple tutorial))
    but it shows the text and ignores a picture(( (for example – smashingmagazine DOT com’s feed)

    help me, pls..

    ps: tx, dcannon, for checking tags)

    01. März 2010 um 01:53 Uhr - AntwortenReply to this comment

Kommentar schreiben

SEOs brauchen es erst gar nicht probieren, werbliche Kommentare (Links zu Shops, Firmen, o.ä.) werden ebenfalls als Spam markiert. Wenn ihr etwas völlig anderes beisteuern wollt, dann bitte über den normalen Kontakt.

oder

neuerer beitrag (h) | älterer beitrag (l)
blogoscoop