How to include a WordPress Blog in another site
February 2nd, 2009 by NikhilThis 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 http://www.inchembur.com/ and you have the news section for this site on http://news.inchembur.com/ ( which is running WordPress). Now the requirement is to show the latest post from http://news.inchembur.com/ on the home page of the main site ,i.e. http://www.inchembur.com/index.php
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.
<?php
define(‘WP_USE_THEMES’, false); // Disbable use of WordPress Theme
require (‘/var/news.inchembur.com/wp-blog-header.php’); // 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 http://news.inchembur.com , 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
query_posts(‘page_id=7’);
query_posts(‘pagename=about’);or You can control the number of posts:
query_posts(showposts=3);
Tags: PHP, Tips & Tricks, WordPress API
Subscribe to ExpressingIT by Email | Follow Me on Twitter