Importing Your WordPress Posts to a Separate Website

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!

4
Shares

20 Comments

  1. How do you change how much of the post is displayed?

  2. Dave says:

    Hi

    I just keep getting several lines of error code. Ideas?

    Thanks

  3. Libby Fisher says:

    Hi Dave,

    Can you please post or email me (libby@thelibzter.com) a link to the site you are working on so that I can look at the files?

    Thanks!

    ~Libby

  4. eric says:

    Thanks so much – very helpful!

  5. Stephen says:

    Thanks for this Libby. It’s working beautifully.

  6. Arpan says:

    By clicking on the home page button I can see your home page. My question is that how did you manage the posts getting change??What is the code behind that?? Thank you.

  7. Tim says:

    I’ve been trying to figure out how to do this for a few days now and then found your blog :) but… it is working except I keep getting this “Warning: ./cache is not writeable. Make sure you’ve set the correct relative or absolute path, and that the location is server-writable.” at the top of the page and instead of being centered my page is now shoved to the left. Can you offer some help? thanks

    • Libby Fisher says:

      Hi Tim,

      That means you should double check the permissions on the folders and files that Simple Pie is using. Please let me know if you need further help.

      Thanks!

      • OrSeep says:

        Hey Libby,

        Please provide more details for “permissions on the folders and files that Simple Pie is using”

        My website is also facing the same issue as Tim said.

        Regards,

        • Libby Fisher says:

          Hi Edward,

          It looks to me like simply a CSS styling issue – your submenus are too far away from the menus so when you move the cursor right to click on a submenu, it moves off of the menu item and the submenu disappears.

        • Libby Fisher says:

          Hi,

          Here is an article that may explain the file/folder permissions issue – http://simplepie.org/wiki/setup/setup. Please let me know if you have any other questions. Thanks!

  8. ayman says:

    i want recent post from my subdomain website on my website master

    example :

    http://subdomain.site.com
    http://www.site.com

    i use wordpress

    how i make that ?

    like this is http://s12.postimage.org/utpz428qj/7_27_2012_2_38_36_AM.jpg

    thanks

  9. John says:

    Hello, followed your instructions. I downloaded the kit and followed their instructions. Added the code to the webpage but it fails at: ‘/simplepie.inc’;

    This does not exist. I searched for it in all the installed simplepie folders but it is nowhere to be seen.

    But thanks for trying anyway.

    Cheers

    • thelibzter says:

      Hi,

      Well did you follow the instructions in the first line of the blog post:

      First, we will download the SimplePie library. Unzip the downloaded file and upload simplepie.inc to your website’s root folder on the server.

      If you did that, there should not be a problem with the simplpie.inc file “not existing.”

  10. Libby Fisher says:

    Hi,

    I would look into this plugin – http://wordpress.org/extend/plugins/rss-import/. I have not used it myself but it looks like it may do what you are looking for.

    Thanks!

  11. Leave a response

    Current ye@r *