This is something I have been trying to figure out how to do for months – and I finally did it!!! So for those of you who are also trying to display your latest blog post (or posts) on a separate website or page of your website – here you go!
First, we will download the SimplePie library. Unzip the downloaded file and upload simplepie.inc to your website’s root folder on the server.
The next thing we need to do is create a php page. (For this tutorial, we’re going to assume you are planning to display your most recent blog posts on your website’s index.html page). The easiest way to do this is to rename your index.html page as index.php. However – before you do this – make sure that you are not now going to create a bunch of broken links from visitors coming from other sites and trying to find www.yoursite.com/index.html. For this reason, I recommend that you make an exact copy of your index.html page and save it as index.php, upload it to your server, and then add this line of code to the head of your index.html page so that visitors are automatically redirected to the index.php page:
<meta http-equiv="refresh" content="0; URL=http://www.www.example.com/index.php">
Change the URL in the code to your index.php file’s URL. (Note: another suggestion I have is that you wait to insert the code to redirect your index.html page until you have the index.php page completely ready to go. This way, visitors won’t see the index.php file as you are working on it, and they won’t get a “Page Not Found” or some other error should things go wrong.)
Next, insert this line of code to the very top of the page where you are trying to display the WordPress posts:
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/simplepie.inc'; $feed = new SimplePie('http://blog.thelibzter.com/feed'); ?>
Obviously, you need to make sure that the addresses to your simplepie.inc file and to the blog feed that you are trying to import are correct.
Now, we simply need to add the imported blog post or posts to the web page!
To display the 5 most recent blog posts, use this:
<h1><?php print $feed->get_title(); ?></h1> <ul> <?php foreach ($feed->get_items(0, 5) as $item): ?> <li> <a href="<?php print $item->get_permalink(); ?>"> <?php print $item->get_title(); ?></a> </li> <?php endforeach; ?> </ul>
To display the entire most recent post, use this:
<h1>Latest post from <?php print $feed->get_title(); ?></h1> <?php $item = $feed->get_item() ?> <h2><?php print $item->get_title(); ?></h2> <?php print $item->get_content(); ?>
Once you have the blog post or posts imported into your web page, you can go about styling the way it displays. The code that I use to import and display my most recent post on my home page is this:
<font size="3"><a href="http://blog.thelibzter.com">Latest from the Blog</a></font> <br/><br/> <?php $item = $feed->get_item() ?> <a href="<?php print $item->get_permalink(); ?>"> <font size="3"><?php print $item->get_title(); ?></h3></a></font> <br/><br/> <font size="2"><?php print $item->get_description(); ?></font>
You can see that I use “font size” to make sure that the imported blog title and description fit comfortably into the div that contains them.
I hope this works for you! If it doesn’t or if you have any issues, let me know and I will try to help you out as best as I can. Enjoy!