So a friend of mine shoots me an email today. She had a front end website, just set of basic pages built with PHP and one of the links on the navigation was a Wordpress blog installed on the same hosting. She wanted to display an RSS feed in the sidebar of the front end website by using a nifty little script called Simple Pie.
Looking over the script, I found it to be a handy little script. It pulls an RSS feed and parses it easily for you and then you can output the parts you want with links to the full article. The only problem I found with what she was trying to do, she just wanted to parse the RSS feed of the Wordpress blog on the same site and display the last 5 articles on the home page of the primary site.
This is easily done with about 8 lines of code. You can get full access to everything Wordpress has to offer from external PHP pages on the same site.
The first thing you need to do is include a Wordpress file in order to load the functions you’ll need access too:
<?php
require_once('./blog/wp-blog-header.php');
?>
The above code is assuming that your installation is in the “blog” directory, naturally change it to reflect your installation. Once this is included in your script you can access all the Wordpress functions. Now if we expand on that code as follows:
<?php
require_once('./blog/wp-blog-header.php');
query_posts('showposts=5'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
Now if you plug through the code you’ll see that we are pulling the last 5 posts (showposts=5) and then we are looping through that information and writing it in HTML. Surround each title with an H2, or your own choice, and create a link with the appropriate Wordpress permalink.
So instead of messing with an entire separate class or script we’ve included the Wordpress functions, then just used the functions to display the information we already wanted. Keep it simple. Use the tools you already have available to you.
The main lesson I find with this example is that there is always 101 ways to skin a cat. The idea is to make sure you are using the tools already available to you, and implementing the solution that keeps it simple. Don’t get focused in on a solution and waste a lot of time, and never hesitate to ask for help.
Related posts:






