Display Most Recent Wordpress Posts On Another Site
HowTo, PHP, WordpressI 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>
Comments
Pingbacks
- Visa dom senaste inläggen på en annan webbplats « Sökoptimering Pingback on Sunday 16th March

For those of us who are more than a little OCD with respect to our code :-) :
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
@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.
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.
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!