Flatcommets incrementing id like on tedserbinski.com?
ak - September 24, 2006 - 08:16
I'd like to display an incrementing id on the comments of a node as a big number next to each comment as you can see on tedserbinski.com. I tried to do this by myselfe but I'm just not getting this to work as I want it to. Searching the forums didn't help either. How can this be done? There does'nt seem to be any variable available to display a comment count either so there must be another way!
Help is very appreciated.

i think there is an
i think there is an additional php code which adds to every comment
<?php<div class="comment-number">$number</div>
?>
and the rest is CSS
look at http://tedserbinski.com/themes/ted/style.css
and learn
div.comment div.comment-number{color:#ccc;font-size:500%;position:absolute;right:0.2em;top:0;z-index:1;}The required css code
The required css code should'nt be a big issue for me. My problem lies in the incrementing number with php. Just incrementing a variable won't work in the comments.tpl.php because it's newly called for every comment and defining a number var as static didn't work either. It's obvious that I'm missing something basic here and it should'nt be all that hard I think. May you have a hint on how I could do this.
ok, look into
ok, look into comment.module, there is a function called comment_render() which shows all comments.
There below a huge comment you have a loop wherein you can put your increments.
I think you should also modify theme_comment_* functions to pass your new variable
Thanks for your suggestion.
Thanks for your suggestion. I looked in to comment_render() and incrementing my number in that loop would probably work, but this would'nt be a desirable solution for me. I don't think hacking up core is the right way to turn for this little feature. I'm trying to achive a solid and flexible solution that can easily be reused by others, a snipped for the handbooks on the theme level or so.
As I was researching the function _phptemplate_variables() can pass variables to my tpl files. This may help in our situation.
I'll report any progress and any helping hand is very welcome.
ok i will check templates
ok i will check templates functions and then i write something
You could wrap the comments with an ordered list
Wrap the comments code in comments.tpl.php with an ordered list and then format the css to place the number where you desire.
php & global
ok another thing is to use php global variable and we operate only on 1 file : comment.tpl.php
it will work only if all comments are available on single page because I used a simple incrementation.
so add some code in comment.tpl.php
<div><?php
global $commentno;
if (!$commentno) $commentno = 1;
print $commentno++;
?>
</div>
PS. but I think it isnt good to place code into view section, we should separate these two things.
maybe is possible to change theme_* functions without changing the core.
Great, this finaly works
Great, this finaly works kinda well your way. Outsourcing the code would be nice. Here's a try to set this up in template.php but somehow the variable does'nt arrive in my comment.tpl.php. But this should be the correct implementation, as described in the handbooks. Maybe somebody can try this out at home and give some feedback if it works?
template.php
<?phpfunction _phptemplate_variables($hook, $vars)
{
switch ($hook) {
case 'comment':
global $comment_number;
if (!$comment_number) $comment_number = 1;
$vars['comment_number'] = '#'.$comment_number++;
break;
}
return $vars;
}
?>
comment.tpl.php
<span class="comment-number"><?php print $comment_number; ?></span>@zoon_unit Would be a nice way to go as well. I'd allthough like to see a implementation like you suggested, just to see how it could be done, but I think it's kind of triky to do, you probably can't build this up in comment.tpl.php, because how and where do you decide when to open and close the
<ol>. And by the way placing the number of an<ol>won't work very well either. So your solution won't be as flexible as the above one on the theming level.this works for me ( drupal
this works for me ( drupal 4.7.3 - bluemarine template )
Great that it works. I'm
Great that it works. I'm using flatforums that adds a chunk of code to the _phptemplate_variables function and somehow my variable gets lost becaus of it.
thanks
Switching the code below the flatforum code made it work, I'm very pleased, thanks a lot for your help.
;-)
cheers mate :)
would be great if we could make a theme with that look of comments.
Does not work on multipage....
If all the comments are on the same page, this is okay, but if you have 11 comments and the display settings limiting 10 per page, the 11th comment will display #1 on second page !!!
more advanced code which
more advanced code which should show proper number on multipage configuration and show specified text or number when we edit or reply .
<?php
function _phptemplate_variables($hook, $vars)
{
switch ($hook) {
case 'comment':
if (arg(0) != 'comment') {
global $comment_number;
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$comments_per_page = variable_get('comment_default_per_page', '50');
$start = $page * $comments_per_page;
if (!$comment_number) $comment_number = 1;
$vars['comment_number'] = '#'.($start + $comment_number++);
}
else {
/* we can show different number when we edit, add comment */
}
break;
}
return $vars;
}
?>
Great work dude, this is
Great work dude, this is realy getting perfect. It was a bit trickier as I thought but here's what I managed on the theme level. Considering the code in your comments.tpl.php:
<span class="comment-number"><?php print $comment_number?></span>this would be the elementary css you would use, including some descriptive comments:
/** make the comments position relative
so we can position the number anywhere within the comment
*/
.comment {
background:#eee;
position:relative;
}
/** we'll place the number on the right hand side for example,
you can subtile ajust the top spacing trough the line-height, change 100%
*/
.comment .comment-number {
color:#fff;
float:right;
font:bold 480%/100% Tahoma, Arial;
position:absolute;
right:.2em;
top:0;
z-index:1;
}
/** position the contents z-index above the number
so our number can be placed everywhere behind the comments content,
we could have a huge number right behind the comments text if we want
*/
.comment .content {
position:relative;
z-index:2;
}
Nice one !!
Works perfectly for me ( 4.7.3 ) !!
Thanks !