Show a list of all user blogs on the site
Drupal comes with a nicely preconfigured block that displays the most recent blog entries, but neither a block nor a menu item that provides an overview of the sites' users with links to each user's own blog. That is, there's no built-in list of the site's blogs rather than blog entries.
These snippets therefore provide a list of user blogs, each configured in slightly different ways. They do not originate with me, but since it really took a while to track them down, here they are for others who need them as badly as I did:
A simple list of user names as links to their blogs:
<?php
global $user;
if($user->uid)
{
print "<ul>\n";
$result = db_query("SELECT DISTINCT u.name, u.uid FROM {users} u INNER JOIN {node} n ON n.uid = u.uid WHERE n.type = 'blog' ORDER BY u.name");
while ($node = db_fetch_object($result))
{
print "<li>".l(ucwords($node->name), 'blog/' . $node->uid)."</li>\n";
$latestr = db_query("SELECT n.title, n.nid FROM {node} n WHERE n.type = 'blog' AND n.uid=%d ORDER BY n.created DESC",$node->uid);
}
print "</ul>";
}
?>A list of user names as links with a digit in parentheses showing how many total posts that user has made to their blog:
<?php
$result = db_query("SELECT u.uid, u.name, COUNT(0) AS num FROM {node} n LEFT JOIN {users} u ON u.uid = n.uid WHERE n.type = 'blog' GROUP BY u.uid, u.name ORDER BY u.name ASC");
while ($blog = db_fetch_object($result)) {
$output .= '<li />'.l($blog->name. ' ('.$blog->num.')', 'blog/'.$blog->uid);
}
return '<ul>'.$output.'</ul>';
?>A more styled list of user names that link to the user's blog, plus the latest blog entry of the user as a leaf-list-item beneath the name:
<?php
global $user;
if($user->uid)
{
print "<ul>\n";
// select all the unique UID "blog" nodes, then join them with users and away we should go
$result = db_query("SELECT DISTINCT u.name, u.uid FROM {users} u INNER JOIN {node} n ON n.uid = u.uid WHERE n.type = 'blog' ORDER BY u.name");
while ($node = db_fetch_object($result))
{
print "<li class=\"expanded\">".l(ucwords($node->name), 'blog/' . $node->uid)."</li>\n";
$latestr = db_query("SELECT n.title, n.nid FROM {node} n WHERE n.type = 'blog' AND n.uid=%d ORDER BY n.created DESC",$node->uid);
if($latestn = db_fetch_object($latestr))
{
print "<ul>\n";
print "<li class=\"leaf\">".l($latestn->title, 'node/'.$latestn->nid)."</li>\n";
print "</ul>\n";
}
}
print "</ul>";
}
?>