December 5, 2007

External Wordpress Function Integration

Filed under: PHP, WordPress — Soleer @ 4:11 pm

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.

Who we are

Explore our history and character.

If you've ever been on a blind-date, odds are great that you know how disastrous "jumping before looking" can be. Feel free to do some online stalking here before you contact Soleer.

What We Do

"Girls only want boyfriends who have great skills." -Napoleon Dynamite

While we aren't looking to be your boyfriend and might not have "nunchuku skills," we do offer a number of other services and solutions to assist your organization.

What We’ve Done

As we say in Texas, "This ain't our first rodeo." Take a look at a sampling of some of our masterpieces, from logo design and brochure creation to webpage design and online community development.