How to display latest wordpress blog posts to non-wordpress site?

23 02 2011

You can easily display your latest wordpress blog posts to your non-wordpress site. For this, first you need to add the below line at the top of your php script-

<?php
require(./path/wp-blog-header.php');
?>

When the above line is added to your php script, it will turn your script into wordpress page, which means, you will be able to use any WordPress template tags in your script. Now we will see how can we retrieve few recent posts from our wordpress blog.

$recent_posts = wp_get_recent_posts(5);

wp_get_recent_posts() function will retrieve latest posts from your blog as an array, by default 10 posts are retrieved. To get any other number of posts, just pass the parameter as – wp_get_recent_posts(5) to get latest 5 posts. Now we will loop through the array to get each individual posts with title and url-

foreach($recent_posts as  $post){
    echo '<a href="' . get_permalink($post["ID"]) . '" title="Look '.$post["post_title"].'" >' .   $post["post_title"].'</a>';
}

Thats all you need to do. So here is the full source code-

<?php
require('./path/wp-blog-header.php');
?>
<ul>
  <li>
    <h2>Recent Posts</h2>
    <ul>
    <?php
      $recent_posts = wp_get_recent_posts(5);
      foreach($recent_posts as $post){
        echo '<li><a href="' . get_permalink($post["ID"]) . '" title="Look '.$post["post_title"].'" >' .   $post["post_title"].'</a> </li> ';
      } ?>
    </ul>
  </li>
</ul>
Advertisements

Actions

Information

One response

30 04 2011
Null0bject

Good tut atik!

I was wondering if you could also explain how to display the date of the post 🙂 Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: