How to import WordPress posts to your HTML website.

Ever thought it would be great to include your latest wordpress post on your html website?

In this tutorial I will describe how the process is done. The WordPress blog and the html site have to be on the same server for this code to work. I haven’t had any luck trying to link to another server with this code.

The first step is to add the code to “require” the header of your WordPress site using PHP. This is a crucial step, requiring the header allows the whole process to work. It will look something like this:

< ?php //Include WordPress define('WP_USE_THEMES', false); require('./wordpress/wp-blog-header.php'); ?>

Place this code at the VERY top of the page above the opening tag. It is also necessary to save the file with a .php extension and not the typical .html or .htm extension.

The second part is to add this bit of code where you want the posts to appear on your website.

<?php $args = array( 'numberposts' => 1 ); $lastposts = get_posts( $args ); foreach($lastposts as $post) : setup_postdata($post); ?>

Note the “1” in (‘numberpost’=>;1) this will display one post only (the newest post). Change this to 2 to post two, 3 for three posts, and so on. I think there is a max of 5 without altering the settings on the actual blog site, check the documentation for more info.

I found it easier to call certain entries from the database with short snippets of code. There are other means to do the same thing with more options, but for my application I just wanted to display the newest post and some related information and use my stylesheet to style it. Here are some of the code snippets that will do the similar things with less code:

<?php echo $post->display_name;?>// The display name of the poster <?php echo $post->post_modified;?>//Displays date the post was last modified < ?php echo $post->comment_count; ?>//Displays the number of comments the post          has up to the present day

One thing I did notice is that IE 8 and below won’t execute the code in step one correctly with the XHTML Transitional doctype, I recieved an error page when I navigated to the URL. To solve the issue, I had to switch to the HTML5 doctype and remove the tag. IE has supported the HTML5 doctype for a long time so this is not much of an issue, but it is semantically incorrect. Also, W3C’s validator finds a few errors when I submitted it for validation (missing “UTF-8 encoding” among others).

Dig around at the WORDPRESS site to find some more info, look for the database section for some advanced features.