WordPress content on non-WordPress pages

You might have a few PHP pages on your website outside of your WordPress installation. In this case, you might want to display a WordPress post/page on a regular .php page, without using themes or anything – you want just the post/page. This tutorial will show you how.

Let us assume that you want to place a post about your dog on pets.php.

Open up pets.php and place the following code at the top of the page. This code basically allows the WordPress content to be displayed:

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

There are a few things you need to change here. You must change wordpress to the folder in which your WordPress files are located. If they aren’t in a folder, simply remove that part.

The next thing – false means we will NOT be using WordPress themes. You might have header/footer includes already, and don’t need the theme at all.

We will come back to query_posts later.

Now, we must add The Loop, which will display the post/page. All strings related to the post/page will basically be placed INSIDE this loop (that’s if you aren’t familiar with WordPress). The Loop will be placed where you want your post/page to appear.

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

Now you obviously want the post to display, so you will have to add strings to display the title, text, date, and so on. You can copy the data from your page.php or single.php file.

Note: You can locate these files through your FTP, or, through the WordPress admin, under Appearance > Editor – and choose the theme to edit. Just copy the basic data from the page.

You might get something like this, after you paste the strings into The Loop.

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

<h1><?php the_title(); ?><br />
<?php the_time('l jS F, Y - g:ia') ?></h1>
<?php the_content(); ?>

<?php endwhile; ?>

Back to query_posts. We have to find the “post number” of the page/post. Find the post number in your WordPress admin. When you look at the URL in the address bar, you will see the post number, for example: http://site.com/wordpress/wp-admin/post.php?action=edit&post=1014.

Replace 1 under p=1 with the number.

In the end your page might look a bit like this:

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

<?php include('header.php'); ?>

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

<h1><?php the_title(); ?><br />
<?php the_time('l jS F, Y - g:ia') ?></h1>
<?php the_content(); ?>

<?php endwhile; ?>

<?php include('footer.php'); ?>

Upload the file to your website, view the page, and you should be able to see that WordPress post/page on a regular .php page.