This PHP code will give your WordPress blog a recent posts list as I have on my sidebar to the right.
It’s not actually a plugin, you need to cut and paste this code into the sidebar.php of your theme.
Recent Posts
<?php
function get_recent_posts ($num_posts = 10)
{
global $wpdb, $tableposts, $post, $tablepost2cat;
if (!isset($tablepost2cat)) $tablepost2cat = $wpdb->post2cat;
if (!isset($tableposts)) $tableposts = $wpdb->posts;
$orderby = "$tableposts.post_$orderby";
$now = current_time('mysql');
$sql = "SELECT DISTINCT * FROM $tableposts ";
$sql .= "WHERE $tableposts.post_date <= '$now' AND ( $tableposts.post_status = 'publish' ";
$sql .= "OR $tableposts.post_status = 'sticky' ";
$sql .= ") ";
$sql .= "GROUP BY $tableposts.ID ORDER BY $tableposts.post_date DESC";
$sql .= " LIMIT 0, $num_posts";
$posts = array();
$posts = $wpdb->get_results($sql);
if (empty($posts)) return;
echo '<ul>';
foreach ($posts as $post) {
$title = the_title('','', false);
echo '<li>';
echo '<a href="'.get_permalink().'" title="View'.htmlspecialchars(strip_tags($title)).'">'.$title.'</a>';
echo '</li>';
}
echo '</ul>';
} //end function get_recent_posts()
get_recent_posts();
?>
Jay Unsworth wrote,
This is a great help! Thanks for this mate
Link | November 19th, 2006 at 6:01 pm
Bock the Robber wrote,
Hi. This is a nice little feature. However, I notice it picks up pages as well as posts. Do you think it could be refined to show posts only?
Link | May 19th, 2007 at 5:15 am
MultiZ wrote,
Thanks,
It works great on my blog. How did you get it to work with comments as well?
Link | July 18th, 2007 at 5:13 pm
Jeremy Myers wrote,
Thanks. This worked perfect and was exactly what I was looking for.
Link | July 26th, 2007 at 3:53 pm
Chris wrote,
Nice, although I have the same problem as Bock the Robber– how can I exclude pages and only include posts??
Link | July 29th, 2007 at 7:41 pm
Scott C. Lemon wrote,
Just a note … I added the following line to your code in order to elimate “pages” from being displayed. On the site that I was working on, I only wanted “posts” to display …
After the line that contains the “WHERE” clause, I added:
$sql .= “AND $tableposts.post_type = ‘post’ “;
Great function! Thanks!
Link | February 9th, 2008 at 12:14 am