Skip to content

Tiny script for returning n last blogposts and excerpt from them.

I just wrote a tiny snippet to show the n last posts from a wordpress blog:

<?php
$num_posts = 5; //Number of posts to show.
require('wp-blog-header.php'); //Wordpress configuration. Change if you don't place this in the root of your wp-installation
$sql = "SELECT `post_excerpt`,`post_title`,`user_nicename`,`user_url`,`$wpdb->posts`.`ID` FROM `$wpdb->posts` INNER JOIN `$wpdb->users` ON `$wpdb->posts`.`post_author` = `$wpdb->users`.`ID` WHERE `post_status` = 'publish' ORDER BY `$wpdb->posts`.`post_date` DESC LIMIT 0,$num_posts";
$arr = $wpdb->get_results($sql);
foreach ($arr as $posts) {
$permalink = get_permalink($posts->ID); //Function to get permalink.
$string = $string . "<h1 class=\"blogheading\"><a href=\"$permalink\">{$posts->post_title}</a></h1> <p class=\"blogauthor\"><a href=\"{$posts->user_url}\">{$posts->user_nicename}</a></p><p class=\"blogexcerpt\">{$posts->post_excerpt}</p>";
}
echo $string;
?>

You can see a demo here. I don’t use excerpts on my blog, so nothing will be shown for the excerpt. Feel free to use as you want. A nicer version of the source can be seen here.

Update: I changed this to show the permalinks.

[Digg] [Reddit] [Google] [StumbleUpon]

Post a Comment

Your email is never published nor shared. Required fields are marked *