Display Wordpress content outside of your blog

Wordpress is a very powerful and flexible content management system (CMS) for blogs, and is among the first choices for anyone looking for a way to include dynamic content on their web sites. When using Wordpress to power a news or blog section of an existing web site, authors will often want to display a list of headlines or the latest post on the front page of the site, outside of the Wordpress-powered section. Using a bit of PHP and the Wordpress API, this is easy to do.

For the purpose of these instructions, I will assume that your web site URL is www.example.com and that wordpress is installed in a subdirectory at www.example.com/blog. I am also assuming a basic level of familiarity with HTML and editing files for the web.

The key to gaining access to the power of Wordpress from an outside page lies in the wp-blog-header.php file. This file loads the Wordpress application and makes its API, and therefor your content, available for use. Once this file is included in a page on your site, you will be able to use any Wordpress function just as if you were working in a Wordpress template.

Let’s add the latest post to the front page of our site. We’ve already got a blog at www.example.com/blog/ but it would be nice to show the latest entry alongside the rest of the information on the front page. If your front page files is already a PHP file, you’re ready to go; otherwise you’ll probably need to rename your front page file extension from .html to .php. (Be sure to test to ensure that doesn’t break anything!)

The first thing we’ll do is pull in that all-powerful Wordpress file, which we can do by adding this section to the very top of the page you want your post to appear on:

<?php
// Include Wordpress 
define('WP_USE_THEMES', false);
require('./blog/wp-blog-header.php');
query_posts('showposts=1');
?>

The first line of this block is simply a comment describing what we’re doing. The second line tells Wordpress not to display your templates, and the third line grabs that file I told you about. The final line performs a database query to retrieve the content we want to display on this page. In this example, we want the single most recent post, but the query_posts() documentation shows how to retrieve a variety of data.

Now we’ll move down in our page and find a good spot for this post. Once you’ve figure out where to put it, we’ll add a Wordpress loop which will display the single post retrieved above:

<?php while (have_posts()): the_post(); ?>
 
<?php endwhile; ?>

Once the loop is in place, we just have to decide what parts of the post to display, and write some HTML for them. Let’s say we want the post title and just an excerpt, followed by a link to the full article. We’d do something like this:

<?php while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<p><a href="<?php the_permalink(); ?>">Read more...</a></p>
<?php endwhile; ?>

If you’ve been writing Wordpress themes, you’ll immediately recognize the template tags used here. We’re using all the same functions for the title, excerpt, permalink, etc, that we do in a Wordpress template. Including wp-blog-header.php makes this all possible.

Bonus

You can even use this code to display blog posts on a completely separate web site, as long as it’s on the same server and you have filesystem access to the Wordpress directory on the original site. Simply modify the require() in the first block on this page to use the full path to your Wordpress installation:

<?php
// Include Wordpress 
define('WP_USE_THEMES', false);
require('/home/corvidworks/web/example.com/blog/wp-blog-header.php');
query_posts('showposts=1');
?>

Note: PHP restrictions such as open_basedir may prevent this last example from working.

Incoming Links

Comments


Hi, great post!

How would you call a specific page as opposed to the latest post.

Thanks!


Easy! Just change the arguments to query_posts() to include the page ID or page slug:

query_posts('page_id=7');
query_posts('pagename=about');

Thanks!

Is there anyway to display the comments as well?


Sure. Once you’ve set your query parameters, this is just a regular Wordpress loop, the same as you’d find on an index or single post page. You’re free to include your comments template as needed.


You’re a beautiful man Ken!


And the least I can do for you is spell your name right Kenn! ;)


Can’t possibly thank you enough for this post. Been looking for this for awhile now. Definitely will give the deserved trackback love.

For anyone hoping to do more than just display the most recent post, you can see a full list of “template tags” below. (Template Tags are functions that you have full access to once you include the wp-blog-header.php file above):

Wordpress Template Tags

Thanks again for the post,
Tison


Hey - thanks for posting this! I knew it would be possible to do this with a bit of simple php - but could I find accurate, up-to-date and concise directions anywhere on the web? Not until I found this page! :)

Thanks very much - it worked a treat for me for one of my friend’s clients… I am sure I will be using this technique many times. Thanks for taking the time to publish it.

I was even able, with my limited php/WP knowledge, to just grab posts from a certain category (next auction) that displays on the last post in the ‘next auction’ page… plus a blog/latest news page that displays the last 4 posts to the blog.

Next mission is to find a good ‘recipe’ for integrating the style for the blog from the main site …


Hi,
I have my WP site all set up.
I have an include file which I feature on all my pages(index.php,archive.php,page.php etc).
How do I display a post on one of my pages?

I tried:

but no success :(
Please help,

Many thanks!


It looks like your code was eaten by the comment form, but for general Wordpress support problems, try the support forums or codex.


Holy crap! I almost gave up looking for this info, until I ran across your article - thanks so much!


Your site looks so nice and clean and the content on this page was so clear and easy to understand. Wordpress itself doesn’t even have this info on it.

Thank you very much for posting it.


Is there any way to control the length of the excerpt?


@Adam: This blog post should help you out. http://incoherentbabble.com/2007/06/08/changing-the-length-of-the_excerpt-in-wordpress/


Adam: Yep, the link isaacw posted is pretty much the same method I’ve used when I need to do this. Thanks isaacw!


Hi!!! Thanks so much for this post Kenn!!!

I have a question that I hope you can answer.
How can I display only like the first two lines of the post instead of a big portion of it? I tryed isaacw link but didn’t work…

Thanks so much,
Paolo


The custom excerpt post linked above does work, but the code shown in the article is broken in that it uses curly “fancy” quotes instead of regular ones, so it can’t be copied+pasted as-is. Fix the quotes and it works fine.

Your comments are welcome

Comment notes

  • Fields marked with a * are required.
  • Your e-mail address will not be published or spammed.
  • Allowed HTML: <a href=""> <b> <blockquote> <code> <em> <i> <ol> <li> <strike> <strong> <ul> <pre lang="" line="">

About Me

Kenn Wilson is a Linux system administrator and web technologist in Oakland, California who spends an inordinate amount of his free time researching and keeping up on the latest web trends and technologies.