Super-Simple Wordpress RSS Syndication For Your Site
If you’re like me you probably have a blog as well as another site. I have found that an effective way to get vistiors to the blog is to link to it from the main site, assuming that that site is more popular. RSS (Really Simple Syndicate) is a way of syndicating your blog to other areas of the web. It is based on XML and generated automatically by Wordpress.
A PHP RSS parser is MagpieRSS. We will be using it in the following tutorial.
- Download the most recent version of MagpieRSS from the above website.
- Upload the “rss” folder to your web server.
- Next we need to make a PHP include file for displaying the posts. Use the following code
<?php require_once('rss/rss_fetch.inc'); //Include the MagpieRSS include file $url = 'http://yoursite.com/blog/?feed=rss2'; //Set a variable containing the URL to your blogs RSS feed $rss = fetch_rss($url); //Retrieve the posts $items = array_slice($rss->items, 0, 5); //Only show the first 5 posts (the second number can be customized to suit your needs) $posts = ''; foreach ($items as $item) { //Loop through the items $href = $item['link']; $cat = $item['category']; $title = $item['title']; $date = $item['pubdate']; $text = $item['content']['encoded']; $text = preg_replace('/<img [^>]+\/?>/', '[image]', $text); //Replace images with [image] $text = preg_replace('/<\/?[^>]+>/', '', $text); //Get rid of HTML $posts .= "<li><a href=$href>$title</a> <em>".substr($text, 0, 100)."</em></li>"; //Print the current post title linked to the page on your blog, and the first 100 chars of the post } echo $posts; ?> - Now you can include that file into your site wherever you want the feed to appear. Enjoy the free traffic!
August 30th, 2008 at 6:43 am
Hi having alittle trouble with implementing this. Im guessing I need to specifiy the ‘content’ ‘title’ and ‘posts’? Im getting an undefined parse error on line 11 and 14.
Instructions are bit slim for me i guess…or at least my ability to follow them..
thanks for any help
August 30th, 2008 at 8:33 am
I fixed the line 11 error ($posts should have been defined first, must have not copied that part). $posts['content'] comes directly from the RSS feed, and would definitely be defined, in the most recent version of Wordpress at least.
September 14th, 2008 at 5:42 pm
Do you have a zip archive of MagpieRSS ?
Can’t open magpierss-0.72.tar.gz
September 16th, 2008 at 7:12 pm
I couldn’t find any after some quick googling.
I use 7-Zip (http://www.7-zip.org/) to open .tar.gz files (and any other kind of archive).