If you have recently added a WordPress blog to an existing static HTML site, you might be wondering how you can display one or more excerpts from your latest blog posts on your home page. In reality, this isn’t nearly as difficult as you might think. All it takes is a couple of easy changes to the code of your home page.
The only real obstacle you’ll need to overcome is the fact that files ending in .html cannot “include” snippets from PHP files by default. The easiest way around this problem is to simply rename your home page from index.html to index.php and then redirect all requests for index.html to index.php by adding this line to the .htaccess file located in the public_html directory:
Redirect 301 /index.html http://www.example.com/index.php
Note: If you are absolutely certain that all internal and external links pointing to your home page refer to the top level domain only (i.e. http://www.example.com) you can skip the 301 redirect step as it will be unnecessary.
If for some reason you simply don’t want to change the home page extension from .html to .php, you can instruct your Apache web server to parse static HTML files as PHP by adding this line to the .htaccess file:
AddType application/x-httpd-php .html .htm
Now we’re ready to add the PHP code for displaying the latest WordPress posts on the home page. Open the file for the home page in your favorite editor and place the following code just before the </head> tag:
<?php
// Include WordPress
define(‘WP_USE_THEMES’, false);
require(‘../blog/wp-blog-header.php’);
query_posts(‘showposts=10’);
?>
Note: This snippet assumes that your blog is located in a directory named /blog. If your WordPress directory is named something different be sure to make the required change to the code above. The snippet also assumes you wish to display excerpts from the last 10 posts. Simply change ‘showposts=10’ to the number of posts you wish to display.
Next, place the following code in the body of your home page where you would like the posts to be displayed:
<?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; ?>
Now you should have a fully functioning “mini-loop” on your home page that displays the designated number of excepts from the latest posts on your WordPress blog. Simply style the posts with CSS to make them match the look of the rest of the page, and you’re done!
About the author: Rick Rouse is the owner of RLROUSE Infoblog.