Community Documentation

Display the (x) most recent weblog entries from a specific user

Last updated October 6, 2006. Created by Dublin Drupaller on May 19, 2005.
Edited by pwolanin, sepeck, cel4145. Log in to edit this page.

PLEASE NOTE! The following snippets are user submitted. Use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

<?php
/**
* the following displays a list of the 10 most recent weblog titles
* and links to the full weblogs of a certain user.
* If you want to increase/reduce
* the number of titles displayed..simply change the $listlength value
*
* for a different user change the $userid value
*
* works with drupal 4.6.
*
*/
$listlength="10";
$userid="8";

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

Comments

Could this be modified

To show it for the "current user", and how would I do that?

Yes, fairly simply

<?php
/**
* the following displays a list of the 10 most recent weblog titles
* and links to the full weblogs of a certain user.
* If you want to increase/reduce
* the number of titles displayed..simply change the $listlength value
*
* for a different user change the $userid value
*
* works with drupal 4.6.
*
*/
global $user;
$listlength="10";
$userid=$user->uid;
if (!
$userid) {
 
$userid = 8; // default in case not logged in. alternately could error out using drupal_access_denied(); return;
}
$output = node_title_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = 'blog' AND n.uid = $userid AND n.status = 1 ORDER BY n.created DESC"), 0, $listlength));
print
$output;
?>

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

Show recent posts for user, broken down by month

I wrote the following block snippet to show the 50 recent blog entries for a user, with "February 2007" style subheadings for each month.
It is designed for a users blog index page, or on a blog posting.

<?php
if(!is_numeric(arg(1)))
    return;  
if(
arg(0)=='node'){
  
$node = node_load(arg(1));
  
$uid = $node->uid;
} elseif (
arg(0)=='blog') {
  
$uid = arg(1);
} else {
   return;
}
$result = db_query("SELECT created, title, nid, status, type FROM {node} WHERE uid = $uid AND status = 1 and type = 'blog' ORDER BY created DESC LIMIT 50");
   
$output = '<ul>';
    while (
$obj = db_fetch_object($result)) {
     
$thismonth = format_date($obj->created, 'custom', 'F Y');
      if(
$thismonth!=$lastmonth){
         if(
$output!='')
           
$output.='</ul>';
        
$output .= '<strong>'.$thismonth.'</strong><ul>';
        
$lastmonth=$thismonth;
      }
     
$output .= '<li>'.l(($obj->title), "node/".$obj->nid).'</li>';
    }
   
$output .= '</ul>';
    return
$output;
?>

Then to make it only appear on a users blog index/blog entry, I used the following PHP snippet in the "Show block on specific pages -> Show if the following PHP code returns TRUE" text area.

<?php
if (!is_numeric(arg(1))) {
   return
FALSE;
}
if (
arg(0) == 'blog')
   return
TRUE;
if (
arg(0) == 'node') {
  
$node = node_load(arg(1));
   if(
$node->type == 'blog')
       return
TRUE;
}
return
FALSE;
?>

Adding blog teaser under titles

These scripts work great still for Drupal 6.x. Any idea on how to get a teaser to print with the title?

Hi thanks the snippets work

Hi thanks the snippets work great. Better than great actually thanks. Would it be possible to do this with Views 3 now do you think?

About this page

Drupal version
Drupal 4.6.x

Archive

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.
nobody click here