Posts Tagged ‘ WordPress API ’

How to include a WordPress Blog in another site

Monday, February 2nd, 2009 By Nikhil

This question crossed my mind a few times before, when I was working on sites earlier too, which used WordPress to show blogs or new kind of content in some part of the site/portal.Being a novice in PHP and around WordPress, I kept procastinating it, thinking ” this isnt my peice of cake”. Finally! this requirement came upto my nose, when started diggin around a bit for solution.
I was surprised to find that it really was easier than I actually thought it was, to display a list of headlines or the latest post on any other page outside of the WordPress-powered section, just using a little bit of PHP and the WordPress API.

Here is what to do :-
For reasons of explanation, assume that your site is and you have the news section for this site on ( which is running WordPress). Now the requirement is to show the latest post from on the home page of the main site ,i.e.

Step1: In your index.php add the following peice of code , Include the WordPress API file. You can add this to the top of the page you want your post to appear on.

define(‘WP_USE_THEMES’, false); // Disbable use of WordPress Theme
require (‘/var/’); // Include WordPress API
query_posts(‘showposts=1′); // Get Latest Post

In the above peice of include , we are getting only the single most recent post. if you want to try more variations feel free to dig into, query_posts() documentation .

Step 2: Now, In the part of the Index/Home page where you want to show the latest WordPress post from , use the following code. Needless to say, feel free to ad your styling divisions, spans and classes as per your design needs.

Step3 : There is no Step 3 … Thats it … you are done!

OfCourse, You can try a few variations as per your requirements ( as I mentioned earlier, refer to query_posts() documentation ) . Heres some taster …

How show a specific post/page as opposed to the latest post:-
This can easily achived by altering the arguments to query_posts() to include the page ID or page slug


or You can control the number of posts:

get ExpressingIT News by Email Subscribe to ExpressingIT by Email or  Follow Me on Twitter

 Subscribe to ExpressingIT RSS
get ExpressingIT News by Email Subscribe to ExpressingIT by Email
 Follow Me on Twitter