A lightweight WordPress page

I had read about this trick before but had never had a need to try it out – until now. So here’s a nifty trick for any self-hosted WordPress installation:

If you want to add a webpage outside of a WordPress installation that has its own design, yet relies on some of the functions of the main WordPress installation (such as the loop), then you can simply create a “WordPress-enabled” page by adding the following PHP statement to the top of the page:

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

By doing this, WordPress will be loaded with the page while the template will not. It will also not show up anywhere in the WordPress site as it is not a post or a page. You can then go ahead and invoke any WordPress core or plugin function and use its data.

In my case, this worked well for an LCD “news display” for our building. I guess, you could also use this approach for a mobile-enabled site (although there are nice WordPress plugins that do this well. In any case, here is a sample of a WordPress-enabled “index.php” file located outside the wordpress installation. To modify it for your needs, simply adjust the path to wp-blog-header.php and you are good to go.

<?php
  /** Loads the WordPress Environment to have access to functions */
  require('../wp-blog-header.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My lightweight wordpress page</title>
  </head>
  <body>
    <h1>News</h1>
    <ul>
      <?php query_posts($query_string . '&cat=3&posts_per_page=5'); ?>
      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
      <li><?php the_title(); ?></li>
      <?php endwhile; else: ?>
      <li>No news at this time.</li>
      <?php endif; ?>
    </ul>
  </body>
</html>

See the image on top of this post for a sample of our installation. You can also look at the “live” version here (optimized for a 720p widescreen LCD TV).