Everyone knows that WordPress is an awesome tool tool for blogging and as well as for website. It can be a powerful CMS to work with. There are times when you need to pull your WordPress content and display it some where other than the blog site. WordPress framework is very powerful and with very few lines of code you can achieve so.
You must have basic knowledge of how wordpress works. You should know how the wordpress functions work. We are going to use some of them in this post.
The most important part is to load or import all the settings and configuration of the wordpress blog. This tricky task is done simply by including this one file called wp-load.php . Once you have this file, you will be able to access the almighty wordpress functions, config variables and just about everything. Now, let us see how can we implement this practically and display the sample posts.
Let say you want to display the latest 5 wordpress posts on to your site, which also has a wordpress blog installed. So if the site is http://mysite.com/ and the wordpress blog is installed at http://mysite.com/wordpress (please replace these domains with the real ones) . Paste the following code in any of the file lets say posts.php,such that the link is http://mysite.com/posts.php
<?php // Include WordPress define('WP_USE_THEMES', false); //exact path for wp-load.php. // This file is kept in the root of wordpress install require('wordpress/wp-load.php'); //Query wordpress for latest 5 posts query_posts('showposts=5'); ?>
Let me dissect the above lines of code for you.
The very first line sets the constant WP_USE_THEMES tells WordPress framework that it should load theme files. This variable is defined in the index.php of the WordPress blog.
In the second line we are including powerful file called wp-load.php which will make the complete WordPress framework available for us.
In the third line we use the WordPress’s inbuilt function called query_posts. This functions queries the database based on the given parameters. In the above lines of code we have specified number of posts to be fetched by using showposts. You can learn more about this query_posts function from the wordpress codex.
We are done with the main work to fetch the posts, now left is to show them. For showing the post we will use yet another powerful wordpress’s technique called posts loop, which look something like below –
<?php while (have_posts ()): the_post(); ?> //your display code goes here <?php endwhile; ?>
So while you are in the loop, all the standard template tags are available viz. the_title(), the_permalink(); . Let us show the posts with title and their short description called excerpt. Paste the following code in the same file called posts.php
<?php while (have_posts ()): the_post(); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_excerpt(); ?> <?php endwhile; ?>
You will see the 5 latest posts from the blog. I have created a demo link right here http://www.digimantra.com/resources/posts.php This link will always show latest 5 posts from Digimantra’s blog.
Complete code to – Display WordPress post content outside the WordPress
<?php // Include WordPress define('WP_USE_THEMES', false); //exact path for wp-load.php. // This file is kept in the root of wordpress install require('wordpress/wp-load.php'); //Query wordpress for latest 4 posts query_posts('showposts=5'); ?> <?php while (have_posts ()): the_post(); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_excerpt(); ?> <?php endwhile; ?>
Following are some tips which will be helpful to change the post listing.
- query_posts(“showposts=5&orderby=rand”) – will show 5 random posts from the blog
- query_posts(“showposts=5&cat=-10,-8) – will show 5 posts but not from category ID 10 and 8
- query_posts(“showposts=5&year=2008”) – will show posts which are published in year 2008
Also, you can show these posts on different website provided that the domain is hosted on the same server. This will help you easily include the wp-load.php file, which in return can successfully access the database.
Hope that helps.
Stay Digified !!