Adding WordPress Posts to Another Site

WordPress sites are now very popular and so many people who have a site that doesn't use WordPress also have one that does (or have WordPress installed in a folder within their main site). Where the two sites are related you may wish to display some information from your WordPress posts on a page of the related site.

Provided that you have both sites on the same hosting account this can be easily done using commands built into WordPress itself to pull the information about the posts you want to display on a page of your other site. There are lots of variants of the code you can use depending on just exactly what you want to display.

The key to linking the two sites together is the wp-blog-header.php file that is in the home folder of the WordPress blog. To link the two sites so you can display blog posts on the other site then you need to include this file into the top of the page that you have on the other site where you want the posts to display. Assuming that you have both sites in folders at the same level within the hosting directory structure and that WordPress is in a folder called 'blog' you would access it from the other site using:


If the folder has a different name or the relationship between the two folders is different then you will need to replace the ../blog/ with whatever the actual path is from the site to WordPress.

You can then make use of WordPress functions within that page to load and display whatever blog posts you want. For example the following code will retrieve the last five posts made on the blog that have been published and display the title and excerpt in the current page.

$posts = get_posts('numberposts=5');
foreach ($posts as $post) : setup_postdata($post);

The get_posts function is very flexible as you can either pass a single value that simply specifies a number of posts or you can define an array and provide multiple values to be a lot more specific as to what you want to retrieve. The following lists all of the available fields you can specify in the array. With this example all of the values have been set to their default values except posts_per_page (which can be used interchangeably with numberposts) and category (which in this example specifies that only posts from the categories with id 1 and 2 are to be retrieved).

<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '1,2',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
$posts_array = get_posts( $args ); ?>

For more information about how the parameters to get_posts() work see get_posts reference.

The calls to the_title() and the_excerpt() retrieve the title of the post and an excerpt from the post respectively. As an alternative to the_excerpt() you can use the_content() to include the entire post content.

Other information about posts that can easily be retrieved via function calls include: the_ID(), the_tags(), the_permalink(), and the_author(). These and many other WordPress functions you can use are described in detail in the WordPress Function Reference.

go to top

FaceBook Follow
Twitter Follow