Last updated 29 March 2012. Created on 11 September 2006.
Edited by neRok, qNqR, arianek, Anonymous (not verified). Log in to edit this page.

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>';
?>

As per the previous code, except this one only shows users who have created a blog post within the last week (7 days * 24 hours * 60 minutes * 60 seconds).

<?php
$result = db_query("SELECT DISTINCT u.uid, u.name, COUNT(0) AS num FROM {node} n LEFT JOIN {users} u ON u.uid = n.uid WHERE n.type = 'blog' AND n.created > (%d-(30*24*60*60)) GROUP BY u.uid, u.name ORDER BY u.name ASC", time() );
while ($blog = db_fetch_object($result)) {
     $output .= '<li />'.l($blog->name. ' ('.$blog->num.')', 'blogs/'.$blog->name);
}
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>";
}
?>

Alternately, in Drupal 6.x the Bloggers module shows a list of bloggers and the titles of their most recent posts.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

zaglick’s picture

I added the " AND u.uid > 1 " to the WHERE statement to prevent User 1's blog from printing

<?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' AND u.uid > 1 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>";
}
?>
mikefairbanks’s picture

Just curious where they code snippets need to go in order to generate this list... I tried pasting it in a basic page with full HTML... but that didn't work and I realized I was clueless here!

Thanks

vishalkhialani’s picture

in the template file :) or enable php in your text field

redcrackle.com

deanochips’s picture

Here is the code i used for Drupal 7 to do the same thing

<?php
    $query = db_select('node', 'n');
    $query->join('users', 'u', 'n.uid = u.uid');
    $query->join('file_managed', 'f', 'f.fid = u.picture');
    $query->fields('u',array('name'));
    $query->fields('n',array('type'));
    $query->fields('f',array('filename'));
    $query->fields('u',array('uid'));
    $query->condition('u.status', 1);
    $query->condition('n.status', 1);
    $query->condition('n.type', 'blog', '=');
    $query->addExpression('COUNT(n.uid)');
    $query->groupBy('u.uid');
    $query->groupBy('n.type');
    $query->orderBy('COUNT(n.uid)', 'DESC');

    $result = $query->execute();

    $output = array();

    while($record = $result->fetchAssoc()) {
    // print $record ;

     $output = '<div class="bloglist_row">';
     $output .= '<div class="bloglist_userimage"><img style="width:100%;" src="./sites/default/files/styles/thumbnail/public/pictures/'.$record['filename'].'"></div>';
     $output .= '<div class="bloglist_row_body"><a href="blog/'.$record['uid']. '">'.$record['name']. 's Blog</a><br />Posts: ('.$record['expression'].')</div>';
     $output .= '</div>';
     $output .= '<img src="blog_spacer.jpg" width="220" height="25" />';

   //  $output .= '<li />'.l($record['name']. ' ('.$record['expression'].')', 'blog/'.$record['uid']);
    }

    echo $output;
?>

and here is some css for it if your feeling lazy

.bloglist_row {
	background-image: linear-gradient(bottom, rgb(218,218,218) 29%, rgb(238,238,238) 72%);
	background-image: -o-linear-gradient(bottom, rgb(218,218,218) 29%, rgb(238,238,238) 72%);
	background-image: -moz-linear-gradient(bottom, rgb(218,218,218) 29%, rgb(238,238,238) 72%);
	background-image: -webkit-linear-gradient(bottom, rgb(218,218,218) 29%, rgb(238,238,238) 72%);
	background-image: -ms-linear-gradient(bottom, rgb(218,218,218) 29%, rgb(238,238,238) 72%);
	background-image: -webkit-gradient(  linear,  left bottom,  left top,  color-stop(0.29, rgb(218,218,218)),  color-stop(0.72, rgb(238,238,238)) );
	border:#b7b7b7 solid 1px;
	border-collapse: collapse;
	border-radius: 7px 7px 7px 7px;
	color: #FFFFFF;
	display: inline-block;
	font-size: 12px;
	height: auto;
	list-style: none outside none;
	padding: 3px;
	width:220px;
	margin:4px;
}
.bloglist_userimage {
	float: left;
	width: 33px;
	background-color: #eeeeee #336699;
	padding-top:2px;
}
.bloglist_userimage img {
	padding:1px;
	border:#CCC solid 1px;
}
.bloglist_row_body {
	color: #444444;
	float: left;
	padding-left: 10px;
	padding-top:2px;
}