Customize Drupal's "recent posts" block?

Drupal

I ran into a problem when I wanted to reduce the number of posts in the default "recent posts" block. This block is created by the blog module and the number of 10 posts is, unfortunately, hard-coded. You could add your blog feed to the aggregator and using its block. You can then specify the number of posts in either the aggregator properties or in the block created by it.

That works for a single blog but for multiple blogs you would need multiple blocks and that's not what I wanted.

I saw a nice snippet on the Drupal site dealing with this.

The core blog module uses the following query, with the number of posts hard-coded:

<?php
$result
= db_query_range(
 
db_rewrite_sql(
   
"SELECT n.nid, n.title, n.created
    FROM {node} n
    WHERE n.type = 'blog' AND n.status = 1
    ORDER BY n.created DESC"
 
), 0, 10
);
?>

Then I saw this snippet: Display a list of (x) most recent blog entries. It basically uses a similar query but uses variables for the type of posts and the number of posts. It works for 5.x and 6.x (the query in the core module has not really changed between versions).

<?php
$listlength
="10";
$nodetype="blog";
$output = node_title_list(
 
db_query_range(
   
db_rewrite_sql(
     
"SELECT n.nid, n.title, n.created
      FROM {node} n
      WHERE n.type = '%s' AND n.status = 1
      ORDER BY n.created DESC"
   
), $nodetype, 0, $listlength
 
)
);
print
$output;
?>

Simply create a new block and set the input type to PHP. Then copy and paste it in there and you're done. Don't forget to change the number of posts from 10 to something else or you won't gain anything.

I know I could have simply linked to the Drupal article, but this blog has become a repository of snippets I use.

Comments

I know you wrote this nearly 3 years ago, but you just saved me hours of time working it out on our first major Drupal website, so thanks very much!

Martin
http://www.moxby.org.uk

You're welcome.