Display Most Recent Wordpress Posts On Another Site
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>

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;
}
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!
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!
http://kayarc.com/blog/2009/02/mysql-query-for-most-recent-wordpress-post/
/*mysql query*/$sql = “SELECT post_date, post_title, post_content, guid, DATE_FORMAT(post_date, ‘%m-%d-%Y %H:%i%s’)AS post_date FROM wp_posts WHERE post_status=’publish’ ORDER BY post_date DESC LIMIT 0,1″;
$query = mysql_query($sql);
$array = mysql_fetch_row($query);
/*variables*/
$post_date = $array['0'];
$post_title = $array['1'];
$post_content = $array['2'];
$post_url = $array['3'];
$latest_blog_post = <<<EOD
/*design view*/
<a href=”$post_url”>$post_title</a><br />
EOD;
/*echo view*/
echo $latest_blog_post;
How do you do this on a NON php site?
I only use PHP so I cannot vouch for other languages. Do some google searches for parsing an rss feed using the language your site is written in.
Thank you so much for this guide! I’m implementing it on some of my addon domains to show the recent posts on my main domain. :-)
Thanks for your script. With this I made a module for PrestaShop (an open source ecommerce solution).
Thanks a lot !
This is fantastic and exactly what I’m looking to do. Do you know a way of pulling the author name also? We use FeedBurner if that matters.