WordPress is an excellent and popular CMS, mainly used by bloggers but also used for managing non-blogging web sites. But in many cases people want to use the WordPress system only as a part of a site, allowing them to use their own code or perhaps another CMS as a frame on the website. So how can we take advance of WordPress’ many functions, such as displaying the latest posts, on a non-Wordpress page? It’s really not difficult, you only need some PHP lines in the pages where you want to integrate WordPress. This guide’ll tell you how.

Just to make it clear, in order to integrate WordPress into your non-Wordpress site, WordPress must be installed somewhere on the same site, for example in a sub-directory (popularily called blog). This site, AcornArtwork, is not managed with WordPress or another CMS. It consist of selfwritten php- and html-pages in different subdirectories and one external CSS stylesheet. But the blog you are currently reading is powered by WordPress, installed in the subdirectory blog. I have created my own theme for WordPress so the blog looks just like it’s a part of the rest of AcornArtwork. This way I can take advance of WordPress’ blog functions for my blog, but can also integrate the blog’s content with my other pages, such as my portfolio subdirectory or front page. If you click into my portfolio in the menu above, you’ll see a sidebar displaying my latest posts that are related to the chosen category. And on the front page you can see a list of the latest posts. These pages do not reside inside WordPress. Enough talk, let’s get to the how!

You need to add some PHP code (just one line actually) in the beginning of your non-Wordpress page where you want to use WordPress data and functions. In this example I assume you’re working on the index.php page and you have WordPress installed in a subdirectory blog. Change the directory path that corresponds to your setup, it should point to the WordPress file wp-blog-header.php that lies in the root directory of your WordPress installation. The path has to be a local or relative directory path, you cannot write an internet address (starting with http://), even though the adress points to the correct place.

<?php require('blog/wp-blog-header.php'); ?>

You can place this line right after the <body>, so it has been initiated right before the page’s content are being read by the browser. Or you can place it right before the PHP codes that calls WordPress functions, it’s up to you, but this line must come before you call any WordPress functions. Test your index.php in a browser and check for any error messages. If the page loads normally, it probably worked fine. You can’t really know for sure until you insert some WordPress functions. Let’s do that.

Edit 17.10.2010: I’ve encountered some issues when using Ajax calls to php-files with the above line. If you find your files returning a 404 error, I suggest you use this instead:

require('./wp-config.php'); 
$wp->init();
$wp->parse_request();
$wp->query_posts();  
$wp->register_globals();
</edit>

You can call any WordPress function at this point, but just to see if you got it right try something as easy as adding a link to your blog’s RSS feed, somewhere after the above line in your index.php:

<a href="<?php bloginfo('rss2_url'); ?>">Blog RSS</a>

If everything went allright, clicking on the link will bring you to your blog’s feed. You fetched the WordPress feed URL on a page outside WordPress, by calling a WordPress function instead of using the direct URL. Nice! Now that we know it works, let’s move on to something more useful, such as displaying your blog’s 5 latest posts. This example only lists the links with a br (new line) between each, so most likely you need to edit the wrapping to your liking, for example inside a html ul list.

<?php query_posts('showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
     <a href="<?php the_permalink() ?>" rel="bookmark" title="Go to <?php the_title(); ?>">
         <?php the_title(); ?>
     </a><br />
<?php endwhile;?>

Or you can do as I’ve done in my portfolio page, display the latest 5 posts from a specific WordPress category (you need the category’s ID). Use PHP to set your desired category (depending on which portfolio category you are currently viewing):

<ul>
     <?php 
     //Add a PHP code to determine desired WordPress category IDs here.
     //For illustration's sake I've set two WordPress categories, IDs 4 and 8.
     $cat = '4,8';

     $myposts = get_posts('numberposts=5&offset=0&category='.$cat);
     foreach ($myposts as $post) :
     setup_postdata($post);
     ?>
     <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Go to <?php the_title(); ?>">
          <?php the_title(); ?></a></li>
     <?php endforeach; ?>
</ul>

As you can see there really is no limit to what you can fetch from your WordPress database on a page outside WordPress. Knowing WordPress functions and WordPress themeing, you can truly integrate your non-Wordpress pages with WordPress’ functionality.

Share this post:
FacebookTwitterEmailShare