
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();
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.







Mike Malinowski
July 1st, 2010 at 02:08
thanks for this tutorial. My website was created when Frontpage was the tool of choice for people interested in easily updated websites that don’t involve high priced consultants. If I were to start today, I’d be using wordpress; but I like your idea of mixing the two platforms. I’m not sure graphically how I’ll marry the two … but you have given me some ideas. Thanks!
Steve
September 12th, 2010 at 00:15
Absolutely Brilliant, just the stuff I have been searching for
Natural Retreats
October 26th, 2010 at 13:16
Great stuff – this worked a treat on the site. Many Thanks
Dave
October 28th, 2010 at 01:50
Wow! Thank you so much. It worked perfectly. Good Job!
Giorgio
January 20th, 2011 at 11:36
Hi,
Great info!
Is it possible to do the same with wordpress pages and not post? Basically i have a contact form page under my wordpress blog and i would like to embed it in my html website, is it possible to use the same method?
Thanks
Giorgio
Rok
February 8th, 2011 at 14:32
That is one of the most useful WordPress tips ever. Worked like a charm. Thank you!
gaurav
June 20th, 2011 at 14:57
it is working !!!!!!!!
thanx a lot….
Anoop
August 27th, 2011 at 19:05
This is one of the bes tip i have ever came across.
Thanks a ton for this great and valuable post…
Seth
September 23rd, 2011 at 02:56
wow, thanks alot for the tut! it took my 2 days of searching to find this tut and i was up and running in 10 mins. thanks again
s1
October 16th, 2011 at 03:36
My page has to be php? Will it work if it is just html?
I copied just under my tag. The file location is correct as blog is where I install wordpress just under my current dir.
I copied to the bottom of my html:
<a href="” rel=”bookmark” title=”Go to “>
But nothing happens.
s1
October 16th, 2011 at 03:38
Let me try that again. I just realized I can’t just copy/paste your instructions on this page.
But I tried exactly as you have suggested but nothing happens. So my question is – does my page has to be index.php? — because my page is index.html
Matt
February 4th, 2012 at 19:32
YAY!!! My hero!! Spent days looking for this, but you made it so simple. Wis I’d found this yesterday.