WordPress is a very powerful and flexible content management system (CMS) for blogs, and is among the first choices for anyone looking for a way to include dynamic content on their web sites. When using WordPress to power a news or blog section of an existing web site, authors will often want to display a list of headlines or the latest post on the front page of the site, outside of the WordPress-powered section. Using a bit of PHP and the WordPress API, this is easy to do.
This article assumes a basic familiarity with modifying WordPress templates, particularly the “loop” API.
A note about terminologyIn this, as well as all my tutorials, I use the domain name example.com to refer to your domain. When you see this, substitute your actual domain name. As well, in this article I will assume you have installed WordPress into a directory called /wordpress. If you use a different name for your WordPress folder, just replace “wordpress” in any paths below with whatever name you chose.
wp-load.phpfile. This file loads the WordPress application and makes its API, and therefore your content, available for use. Once this file is included in a page on your site, you will be able to use any WordPress function just as if you were working in a WordPress template.
Let’s add the latest post to the front page of our site. We’ve already got a blog at
www.example.com/wordpress/but it would be nice to show the latest entry alongside the rest of the information on the front page. If your front page is already a PHP file, you’re ready to go; otherwise you’ll probably need to rename your front page file extension from
.php. (Be sure to test to ensure that doesn't break anything!)
The first thing we’ll do is pull in that all-powerful WordPress file, which we can do by adding this section to the very top of the page you want your post to appear on:
// Include WordPress define('WP_USE_THEMES'false require('./wordpress/wp-load.php'); query_posts('showposts=1'); ?>The first line of this block is simply a comment describing what we’re doing. The second line tells WordPress not to display your templates, and the third line grabs that file I told you about. The final line performs a database query to retrieve the content we want to display on this page. In this example, we want the single most recent post, but the
query_posts()documentation shows how to retrieve a variety of data.
Now we’ll move down in our page and find a good spot for this post. Once you’ve figure out where to put it, we’ll add a WordPress loop which will display the single post retrieved above:
<?php while (have_posts()): the_post(); ?> <?php endwhile; ?>
If you’ve been writing WordPress themes, you’ll immediately recognize the template tags used here. We’re using all the same functions for the title, excerpt, permalink, etc, that we do in a WordPress template. Including
<?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; ?>
wp-load.phpmakes this all possible.
BonusYou can even use this code to display blog posts on a completely separate web site, as long as it’s on the same server and you have filesystem access to the WordPress directory on the original site. Simply modify the
require()in the first block on this page to use the full path to your WordPress installation:
Note: PHP restrictions such as
<?php // Include WordPress define('WP_USE_THEMES', false); require('/var/www/example.com/wordpress/wp-load.php'); query_posts('showposts=1'); ?>
open_basedirmay prevent this last example from working.
UpdateUpon further research, I’ve discovered that this technique cannot be used to pull content from more than one WordPress site. If you need content from more than one, try using the RSS feed from the second (third, etc). You won't get full API access the way you will using this method for the first, but at least it will get you headlines and content. See my article on displaying RSS feeds with SimplePie if you need help with this.
Source : http://www.corvidworks.com/
Related Article : http://www.fldtrace.com/wordpress/display-latest-post-outside-of-wordpress-with-json-and-jquery