Displaying WordPress Post/Page Content Outside Blog Directory

[REF: http://codex.wordpress.org/Template_Tags/query_posts%5D

The WordPress web logging application is a versatile tool that can (some assembly required) act as Content Management System (CMS) for developing websites.

The installation itself is quite flexible in terms of directory and path. A simple, website project consisting of a set number of static pages easily becomes a multifacted production with the implementation of automatically generated posts. What about including this a WordPress post and/or page that does not lie in the root directory?

In order to call upon portions of WordPress the following code needs to be placed at the beginning of a PHP file.

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

Once this code is processed, PHP can be used to insert the content from a particular post or page with the following.

<?php
// retrieve one page with an ID of 70
query_posts('page_id=70');
global $more;
// set $more to 0 in order to only get the first part of the post
$more = 0;
// the Loop
while (have_posts()) : the_post();
  // the content of the post
  the_content('Read the full post»');
endwhile;
?>

Here is some code to display recent posts (by title) with links:

<?php $current_month = date('m'); ?>
<?php $current_year = date('Y'); ?>
<?php query_posts("cat=13&year=$current_year&monthnum=$current_month&order=ASC"); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<ul>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link"><?php the_title('<li>', '</li>'); ?></a>
</ul>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Advertisements

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s


%d bloggers like this: