Request quote

How to display recent posts outside WordPress

Posted on: September 17th, 2011 by Mohammed Aqeel 3 Comments

Ever want to display your latest wordpress blogs on a different site? With a little PHP you can simply hook into WordPress and retrieve your recent posts. As you are gaining access to all of WordPress’ functions, you have full control over how many posts to fetch and from which categories amongst other things.

Php Script

<?php require($_SERVER['DOCUMENT_ROOT'] . '/wordpress/wp-load.php'); query_posts('showposts=3'); if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
Posted on < ?php the_time('l jS F, Y') ?> < ?php the_excerpt(); ?>
<?php endwhile; else: echo "no posts"; endif; ?> < ?php wp_reset_query(); ?>

How it works

  1. Simply copy the code above, and paste onto your non-Wordpress web page.
  2. Adjust the path to wp-load.php on line 2. In the example, the folder is called ‘wordpress’ – you will need to change this to match the name of your folder, if different to ‘wordpress’.
  3. Decide how many posts you would like to feed through. If anything other than 3, simply locate ‘showposts=3? on line 2, and adjust accordingly.
  4. Upload and test.
  5. Adjust the surrounding HTML as necessary.

Don’t forget that this method only works for pages that are on the same domain as the WordPress installation.

More tweeks

Get posts from particular category

The above code, by default, will get the latest posts from all WordPress categories. If you would like the posts to be from a specific category, you first need to find out that category’s ID. To find out a categories ID, simply:

  1. log into your WordPress Admin Dashboard
  2. Click on ‘Categories’ under ‘Posts’ in the left-hand menu
  3. Hover your mouse over the category for which you need to know the ID of
  4. If you look in the Status Bar of your web browser, you should be able to see that the category’s ID is contained at the end of the target URL.

Once you have the category ID (let’s say ’2? for example, you need to modify line 2 of the script as follows:

<?php require($_SERVER['DOCUMENT_ROOT'] . '/wordpress/wp-load.php'); query_posts('cat=2&showposts=3');  if (have_posts()) : while (have_posts()) : the_post(); ?>

You will see that the category (8) has been defined inside the ‘query_posts’ function, alongside how many posts we would like to display. These two variables must be separated with an &.

For more info and reference visit:

Tags: , , , , , , , , ,

3 Responses

  1. info says:

    Thanks for this info. It saved my day.

  2. Ken Hook says:

    I’ve looked at a lot of posts describing displaying wp posts to a non-wp site and your directions seem the easiest to follow for non-pros.
    Thanks for the post….I will try it!

  3. Webgalli says:

    Glad to know its useful for you.