Display Most Recent Wordpress Posts On Another Site

23 January, 2008 (13:16) | HowTo, PHP, Wordpress | By: Matt | Digg (0)

I was recently had the job of displaying the most recent Wordpress posts on a sites main page. The easiest way I could think of doing this is to use the RSS feed.

I’ll give two sample php functions that will do this as one requires some pear packages and the other doesn’t.

Option 1

This option requires the use of several PHP Pear packages. Those packages are XML_RSS, XML_Tree and XML_Parser. This is the preferred option as the code is specific to RSS instead of XML generically.

< ?php
require_once "XML/RSS.php";
 
// read_rss(display_n_items,feed_url)
function read_rss($display=0,$url='') {
    $rss =& new XML_RSS($url);
    $rss->parse();
    $itemArr = array();
    foreach ($rss->getItems() as $item) {
        if ($display == 0) {
            break;
        }
 
        array_push($itemArr,$item);
 
        $display--;
    }
    return $itemArr;
}
?>

Option 2

This option does not require any special Pear packages which would be helpful for users who do not have the capability to install them or have their hosting provider install them.

< ?php
// read_rss(display_n_items,feed_url)
function read_rss($display=0,$url='') {
    $doc = new DOMDocument();
    $doc->load($url);
    $itemArr = array();
    foreach ($doc->getElementsByTagName('item') as $node) {
        if ($display == 0) {
            break;
        }
 
        $itemRSS = array (
            'title'       => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'description' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link'        => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'pubdate'     => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
        );
 
         array_push($itemArr, $itemRSS);
 
        $display--;
    }
    return $itemArr;
}
?>

Now to use either of these functions we would do something similar to the following:

<ul>
< ?php
$items = read_rss(2,'http://sivel.net/feed');
foreach ( $items as $item ) {
    echo '<li><a href="' . $item['link'] . '">' . $item['title'] . '</a>';
}
?>
</ul>

Possibly Related Posts:


Comments

Comment from Rob Wilkerson
Time January 23, 2008 at 1:37 pm

For those of us who are more than a little OCD with respect to our code :-) :

function read_rss($display=0,$url='') {
    $rss = new XML_RSS($url);
    $rss->parse();
    $itemArr = array_slice ( $rss->getItems(), 0, $display );

    return $itemArr;
}

Comment from Global Fusion
Time March 17, 2008 at 1:45 pm

Hi there,

I’ve been looking for a code like this one that will allow me to show my latest posts in our homepage.

I am wondering if you can provide more details on how ti implement it. Will be very nice to have it working.

Thanks

Comment from Matt
Time March 20, 2008 at 8:41 pm

@Global Fusion: First, the page that you want to display the most recent posts must be a php page (ie ending in .php and not .html, .htm, or the likes)

Second, place the first snippet of code listed anywhere in your page. Usually you would put this at the very top.

Third, place the code in the third code box into your page where you want it to display.

That is pretty much it.

Comment from Global Fusion
Time March 21, 2008 at 8:14 pm

Matt,

Thanks a lot for your response. I am trying to figure this out since our ecommerce site is based on php and smarty. So home.php calls up home.tpl which is the main template for the shopping cart. Within home.tpl there are different {include file=”xxx/xxxxx.tpl”}, and one of them happens to be the one I want to replace with your blog snippet. The reason is that our blog is updated more frequently that the news mod we got

I’m going to ask for feedback in the shopping cart forum and see how we can make this happen. If you happen to be familiar with x-cart shopping cart, your feedback will be appreciated.

Again, thanks for your help.

Comment from Catherine
Time May 31, 2008 at 11:38 pm

Hi - is it possible to use your code to also pull in a thumbnail image from another wordpress blog? It’s one that I also run, so there are no copyright issues. :-) It’s on the same server in fact, just a different account.

Thanks!

Comment from Matt
Time September 24, 2008 at 4:12 am

Hi Matt!

Exactly what I need on my site. Thanks :) Only things extra I need, if possible, is the post image thumbnail and post text (not whole text, just the first little part of article) to be included with the link.

Thanks again!

Trackbacks/Pingbacks

  1. Visa dom senaste inläggen på en annan webbplats « Sökoptimering

Write a comment




Please place code inside of <code></code>.

Please do not post support or bug type discussions for WordPress plugins here. Support type discussions for WordPress plugins should be posted at http://forum.sivel.net/.

Comments are moderated and will not be approved if they contain support discussions.