9 January, 2014 — Development, Snippets, WordPress

Create a “Recent Posts” Module Outside of WordPress

Not all websites run on WordPress, but some websites might decide to use WordPress as their blogging choice. They decide they want to display the recent posts from the blog on the website. However the website isn’t in the WordPress core. So how do we display the recent posts on the website?

The Code

To be able to use WordPress outside of the WordPress core is simple. Just include the 

wp-load.php
 file in your website:

// Include the wp-load from WordPress Install
include('wp-load.php');

// Get the last 10 posts
$recent_posts = wp_get_recent_posts(array(
    'numberposts' => 10
));

// Output the posts
echo '<ul>';
foreach($recent_posts as $post) {
    echo '<li><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></li>';
}
echo '</ul>';

Once you have the 

wp-load.php
 file included, the entire WordPress core is open to use!