Posted by & filed under Programming & Sysadmin, Web development.

So you want to add WordPress to a plain html/css site?
You have 2 options:

  1. Convert your site to a WordPress Theme, and enable all of the wonderful world of WordPress.
  2. Use WordPress to add basic CMS ability, but skip all the bells and whistles of the blog engine.

This Post deals only with the second option.

Here’s a quick how-to for Adding WordPress to an Existing Site as a CMS only.

  • Install WordPress into a new empty directory (installing WordPress is covered elsewhere).
  • Decide what parts of your site you wish to manage using WordPress. WordPress works well for managing a variety of content types.
  • Organize your content as Categories containing multiple Posts – think web pages with multiple paragraphs. Or, as single html fragments you will save as individual Posts.
  • Links that you want to manage can be easily managed using the WordPress Link Manager.
  • Images, can be managed using the WordPress Media Manager.

Once you have a basic idea of how to structure your content in WordPress, you just need to replace your html pages with php pages that use WordPress to show your content. This is easier than you might think!

For example, a web page with multiple paragraphs and headings requires replacing those paragraphs and headings with this code:

<?php 
define('WP_USE_THEMES', false); 

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

if ( have_posts() ) : 
  $readposts = get_posts('cat=3&orderby=meta_value_num&meta_key=priority&order=ASC&numberposts=50');
  foreach($readposts as $post) : setup_postdata($post); ?>
    <p><?php the_title(); ?></p>
    <div><?php the_content(); ?></div>

  <?php endforeach; endif; ?>

This code above does the following:

  1. Tell WordPress that we are not using a Theme.
  2. Load the required WordPress header (adjust to where you installed WP).
  3. Get all the Posts in Category 3 (the category ID assigned by WordPress).
  4. Display them sorted by custom field named ‘priority’. When you add your content to WordPress, you will need to add this custom field to each Post that you want shown here. Assign the paragraphs (the Posts) a priority based on the order you want them displayed.
  5. Style and display the post title and the post content from WordPress!

That’s it!

OR, if you just want to grab a single post, you can modify the above lines to this:

 <?php 
 define('WP_USE_THEMES', false);
 require('wp/wp-blog-header.php');
 query_posts('p=153');
 while(have_posts()) { 
 the_post(); ?>
 <div><?php the_content(); ?></div>
 <?php } ?>

In the above code, ‘153’ is the Post ID in WordPress.

OR, if you want to grab a Link from WordPress Link Manager:

<?php $bm = get_bookmark(5); echo $bm->link_url; ?>

In the above code, ‘5’ is the link ID in the WordPress Link Manager.