Community Documentation

Show a list of all user blogs on the site

Last updated March 29, 2012. Created by schwa on September 11, 2006.
Edited by neRok, qNqR, arianek, Quartz. 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.

Comments

Updated with code to prevent admin's blog from printing

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>";
}
?>

Website Administrator
Syracuse City School District
SyracuseCitySchool.com

Just curious where they code

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

in the template file :) or

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

About this page

Drupal version
Drupal 4.7.x, Drupal 6.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here