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:







{ 2 comments… read them below or add one }
Hi Ed,
Thank you very much for providing the article. I know this post is rather old, but I hope you might be able to help me out with implementing the script you’ve outlined. I’m not having any success with the require_once() method, and I am not sure what the problem is, since it seems so straightforward.
At the moment, I’m using JQuery to make an Ajax call to “custom_functions.php”. I don’t want to add the new function definitions directly to the theme’s original “functions.php” because I need to pass specific information asynchronously and have the server return new DOM elements based on the information given it.
The error I see is:
“Fatal error: Call to undefined function query_posts() in /..filepath../custom_functions.php on line 23″
I know that the line “require_once(‘./filepath…/blog/wp-blog-header.php’);” is being executed correctly, since there is no fatal error caused by the require_once() method (I kept getting those until I correctly inputted the link). At least I THINK it’s safe to assume that the wp-blog-header.php is being imported into the new document.
Do you have any ideas on how I can fix this? I am at a complete loss. Any help would be greatly appreciated!
Could you tell me a little more about the JQuery/AJAX call you are making to the Custom_functions.php file?
There are several references on the web related to the require_once() implementation having some issues. You will notice the solution in my post is all self contained. You may have an issue with Wordpress Globals not being passed outside the active function.
Here is one such reference: Calling wp-blog-header.php from inside a PHP function