If you don’t want WordPress loop
So you don’t want to take advantage of WordPress loop? Then you can usewp_get_recent_posts(). According to WordPress codex:
wp_get_recent_posts() will return a list of posts. Different from get_posts which returns an array of post objects.
How to use it?
Just copy paste following code in page where you want to list the Recent Posts
include( 'wp-load.php' );
// Get the last 10 posts
$recent_posts = wp_get_recent_posts( array(
'numberposts' => 10,
'post_type' => 'post',
'post_status' => 'publish',
) );
// Display them as list
echo '<ul>';
foreach ( $recent_posts as $post ) {
echo '<li><a href="', get_permalink( $post['ID'] ), '">', $post['post_title'], '</a></li>';
}
echo '</ul>';
You can simply change numberposts parameters value to change the number of posts you want to retrieve.
If you want to take advantage of WordPress loop
So if you want to take advantage of the WordPress loop then you can useget_posts() to retrieve posts from your WordPress blog. Note: query_posts() is strongly discouraged — it modifies the global $wp_query object, which can break pagination and other functionality on the page. Use get_posts() or WP_Query instead.
define( 'WP_USE_THEMES', false );
include( 'blog/wp-load.php' );
// Using get_posts() — recommended over query_posts()
$args = array(
'numberposts' => 5,
'post_status' => 'publish',
);
$recent_posts = get_posts( $args );
// Display them as list
echo '<ul>';
foreach ( $recent_posts as $post ) {
echo '<li><a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></li>';
}
echo '</ul>';
In both cases you might have noticed that we are using wp-load.php to get connected with WordPress installation. Once that file is included all WordPress functionality will be available to you. That means you can interact in any way as you wish with your WordPress installation.
Note:Since
wp-load.php is included in your page which is outside of WordPress, you may face error with some of WordPress plugins and cache. To turn off cache for that page on which you just want to show posts you can add define('WP_CACHE', FALSE); just after include('blog/wp-load.php');.