23Jan

Display Most Recent Wordpress Posts On Another Site

HowTo, PHP, Wordpress

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>
6 comments

Comments

  1. Gravatar Posted by Rob Wilkerson on Wednesday 23rd January

    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;
    }
  2. Gravatar Posted by Global Fusion on Monday 17th March

    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

  3. Gravatar Posted by Matt on Thursday 20th March

    @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.

  4. Gravatar Posted by Global Fusion on Friday 21st March

    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.

  5. Gravatar Posted by Catherine on Saturday 31st May

    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!

Pingbacks

  1. Visa dom senaste inläggen på en annan webbplats « Sökoptimering Pingback on Sunday 16th March

Leave a reply