Recipe for two column block of recent comments
My site http://www.delriolive.com has rather large CSS "pods" to display certain blocks. One such block is recent comments. The default "Recent Comments" block is too narrow for my large block size, so I embarked to create two columns of recent columns with 10 recent comments in each column. Using the phptemplate technique, here's how I did it in Drupal 4.7:
1. Find the comment block function in modules/comment.module:
function theme_comment_block() {
$result = db_query_range(db_rewrite_sql('SELECT c.nid, c.subject, c.cid, c.timestamp FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC', 'c'), COMMENT_PUBLISHED, 0, 10);
$items = array();
while ($comment = db_fetch_object($result)) {
$items[] = l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid) .'<br />'. t('%time ago', array('%time' => format_interval(time() - $comment->timestamp)));
}
return theme('item_list', $items);
}2. Cut-n-paste the entire function into theme/simplex/template.php (my theme in this example is "simplex." Replace your theme name with appropriate theme name). Then modify the mysql query gather 20 instead of 10 recent comments:
$result = db_query_range(db_rewrite_sql('SELECT c.nid, c.subject, c.cid, c.timestamp FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC', 'c'), COMMENT_PUBLISHED, 0, 20);3. Rename the function to correspond to the simplex theme:
function simplex_comment_block() {
...
}4. Change the elements returned by the function to parse through phptemplate using the _phptemplatecallback function. Note that we are going to parse just the comments, so I renamed the array to be passed as 'comment_block,' NOT 'item_list.' Because of this, instead of creating an items_list.tpl.php file, I will create a comment_block.tpl.php and place it in the themes/simplex directory:
function simplex_comment_block() {
...
return _phptemplate_callback('comment_block', array('items' => $items));
}5. Create a comment_block.tpl.php file where we will format the 20 comments that are passed as an array called $items[]. To place the comments in two columns with 10 comments in each columns in CSS, I used the following code in comment_block.tpl.php:
<div class="leftfrontcommentblock">
<?php
$ni=0;
foreach ($items as $aa) {
if ($ni < 10) {
print $aa . "<br />";
$ni++;
}
elseif ($ni== 10) {
?>
</div>
<div class="rightfrontcommentblock">
<?php
print $aa . "<br />";
$ni++;
} else {
print $aa . "<br />";
$ni++;
}
}
?>
</div>6. The two columns of comments are inserted into an existing CSS DIV tag and need to lie side-by-side to make the two columns. Here is the CSS that accompanies the output of comment_block.tpl.php:
.leftfrontcommentblock {
float: left;
width: 192px;
height:260px;
margin: 5px 5px 0 0;
padding: 0;
background: #fff;
font-size: 9px;
}
.rightfrontcommentblock {
float: right;
width: 192px;
height:260px;
margin: 5px 5px 0 0;
padding: 0;
background: #fff;
font-size: 9px;
}If you have any comments, or better way to do this, please post it here! Thanks! Joe Hyde joeghyde at gmail dot com.
