I am listing all the comments in a page.

For example, suppose the comments are listed using a design similar to the following one:

Comment Title 1

Comment Description 1

Comment Title 2

Comment Description 2

Comment Title 3

Comment Description 3

If the third comment is the last comment, I should not print the line for that comment.

How can this be done?

5 Answers 5


EDIT: Updated to use a preprocess function. This still might not be pretty, but works fine. In your template.php file in your theme, add this function (replacing YOURTHEME with the name of your current theme):

function YOURTHEME_preprocess_comment(&$variables) {
  $display_order = _comment_get_display_setting('sort', $variables['node']);
  if ($display_order == 1) {
    $variables['end_comment'] = db_result(db_query('SELECT MIN(cid) AS cid FROM {comments} WHERE status = 0 AND pid = 0 AND nid = %d', $variables['node']->nid));
  else if ($display_order == 2) {
    $variables['end_comment'] = db_result(db_query('SELECT MAX(cid) AS cid FROM {comments} WHERE status = 0 AND pid = 0 AND nid = %d', $variables['node']->nid));

Now copy comment.tpl.php from modules/comment and place it in your theme folder. Add this code

print $end_comment == $comment->cid ? ' last' : '';

to the class area of the div html. It should look something like:

<div class="comment<?php print ($comment->new) ? ' comment-new' : ''; print ' '. $status; print $end_comment == $comment->cid ? ' last' : ''; ?> clear-block">

You now have a class of last on the bottom comment, no matter which way they are sorted. Now simply add some simple CSS to your stylesheet:

div.comment.last {
  border-bottom: none;

You can also use jQuery or CSS to remove the bottom border from the last comment, but they are less reliable depending on a user's browser, browser settings, etc.


// Remove border from last comment
$('#comments div.comment:last').css('borderBottom', 'none');

CSS (Works in IE9, but unfortunately will not work with IE8 and below):

div.comment:last-child {
  border-bottom: none;
  • Thanks for your answer laxman... Even it doesn't work dude...
    – Fero
    Commented Jun 23, 2011 at 5:38
  • @Fero did you clear the cache after doing this?
    – Laxman13
    Commented Jun 23, 2011 at 13:01

If you work on D7 :

  • There's no last class attributed to the last comment of a node. So normally you can't. Maybe you can do some tricky CSS attribution :

Let say your line is a border-bottom : 1px solid #ccc; attributed to each div.comment, then you may replace it by a border-top : 1px solid #ccc; and "erase" it for the first comment wrapper But once again, the comment ID doesn't appear in the class...

If you don't have any title for all the comments, try something like div.comment-wrapper { border-top : none; }.

I don't see much solution, except the dirty one : rewrite partially the comment core module to add first and last class to the first and last comments for each node... :(

EDIT : Actually, I've looked at Heihachi's answer and I think I found the solution :

  • create a custom comment.tpl.php (copy paste it from the core original files).
  • add a div with a specified class to wrap <?php print $content ?> something like <div class="comments-content"><?php print $content ?></div>
  • add the following css : .comment { border-top: 1px solid #D1D1D1; } .comments-content { margin : 0 ; padding : 0; border-top: 1px solid #fff; /* This border will normally hide the first comment line. Make it the same color as your background */ }
  • Thanks for your reply nyoz.. But i am working with D6
    – Fero
    Commented Jun 21, 2011 at 11:38
  • Could you give us the CSS you use to make the border / line appear ? Thks!
    – nyoz
    Commented Jun 21, 2011 at 13:12
  • @@nyoz: Here it is... border-bottom: 1px solid #D1D1D1;
    – Fero
    Commented Jun 21, 2011 at 13:49
  • ANSWER UPDATED. See my original post.
    – nyoz
    Commented Jun 21, 2011 at 14:24
  • Did that worked?
    – nyoz
    Commented Jun 22, 2011 at 15:58

Is this on a node's page... displaying the comments for the node? Or is this a custom page you created that displays all the comments?

If this is a node displaying the comments for that node, then D6 does have have last CSS class on the last comment. You can use that in your CSS to remove the bottom border on the last comment.


If you want to remove the bottom line of the last comment:

  1. Add the preprocess_comment function in you template.php file:

    function *YOUR TEMPLATE*_preprocess_comment(&$vars){  
      if($vars['id'] == $vars['node']->comment_count){
        $vars['classes_array'][] = 'last';
  2. Add in your CSS file:

    div.comment-item.last { border-bottom: 0 none; }

If you use a D6: In a comment.tpl.php delete all previous code and paste this:

<h3><?php print $title ?></h3>
<div class="commentz"><?php print $content ?></div>

and in css:

.commentz { border-bottom:1px solid #000; }
  • not working....
    – Fero
    Commented Jun 21, 2011 at 12:50
  • That would add a border to the bottom...
    – Laxman13
    Commented Jun 21, 2011 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.