External WordPress Function Integration

We recently came across the need to display recent posts in an area that wasn’t part of our wordpress blog. This is how we solved the problem:

I’m sure this is a common need for many people who have a index (home/front) page, where they would like to display teasers of recent blog entries. Most of the websites we develop have needs beyond those provided by the standard WordPress Capabilities. WordPress is a great blogging platform, but for most of our projects we like to restrict its use to simply blogging.

WordPress includes a Recent Posts Plugin, which provided the functionality we needed. It creates a function called “mdv_recent_posts().” After activating this plugin, all you have to do within your theme is call this function, and it will return nicely formatted recent posts.  (This part works great)

The problem is that we were attempting to do this from our front page which wasn’t a part of the blog.  The blog was contained in its own directory.

The Solution:

So, to do this fix, you’ll need to edit the php code of the specific page.  In our case it was ‘index.php’.

In the head region, you’ll want to add:

<?php
     define(‘WP_USE_THEMES’, false);
     require(‘../blog/wp-blog-header.php’);
?>

The first ‘define’ statement lets WordPress known that we are handling the page design ourselves, and it doesn’t need to theme it.  The second ‘require’ statement includes the wp-blog-header( ) function, which then includes all of the necessary WordPress variables and functions.

Now that our page has access to the insides of WordPress, all we need to do is call the function.  In the area of the page that you want to include the recent posts block, simply include the php statement:

<?php mdv_recent_posts( ); ?>

…and voila!  Now you can access WordPress’s widgets and functions on the pages of your website that aren’t part of WordPress.