...

View Full Version : Trying to add numbered comments in the comment section with $commentcount++



cpkid2
02-02-2010, 09:07 PM
I'm building this site on Wordpress and I'm trying to make the comments numbered for the comments section by setting a variable and using $commentcount++ . You can see what I mean here:

As you can see, all of the numbers are "1" when they should be adding up as new comments get submitted. What am I doing wrong?

Here's what I have at the top of comments.php:


<?php

// Do not delete these lines
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!');

if ( post_password_required() ) { ?>
<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
<?php
return;
}

$commentcount = 1;

?>

Here's what I put in my functions.php file:


function format_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; $commentcount = 1;?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<span class="comment-number">
<?php echo $commentcount++;?>
</span>
<div class="comment-avatar">
<?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?>
</div>
<div class="comment-text">
<div class="comment-author">
<?php printf(__('<span>%s</span>'), get_comment_author_link()) ?>
</div>
<div class="comment-date">
<?php echo get_comment_date(); ?>
</div>
<?php if ($comment->comment_approved == '0') : ?>
<em><?php _e('Your comment is awaiting moderation.') ?></em>
<?php endif; ?>

<?php edit_comment_link(__('(Edit)'),' ','') ?>

<?php comment_text() ?>
</div>
<?php
}

Any help would be appreciated. Thanks in advance.

angst
02-02-2010, 09:13 PM
where is the script the loops through the comments to display them?

format_comment() only displays one comment at a time. so each time you are resetting the counter back to 1.

$commentcount = 1;

cpkid2
02-03-2010, 02:04 AM
The script that loops through the comments is the function.php file. I put "<?php echo $commentcount++;?>" on line 5.

JAY6390
02-03-2010, 02:12 AM
Change

$commentcount = 1;
// to
static $commentcount = 1;

cpkid2
02-03-2010, 02:27 AM
^I just tried that but it didn't work.

Len Whistler
02-03-2010, 04:13 AM
I would put the counter in one file, not in two files. It makes it easier to debug. The counter has to be setup like:


DB Query
$counter = 1;

while loop {
echo $counter
echo db data
$counter++;
}




---------

thekooliest
02-03-2010, 10:11 AM
<?php

$commentcount = 1;

// Do not delete these lines
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!');

if ( post_password_required() ) { ?>
<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
<?php
return;
}

?>




function format_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<span class="comment-number">
<?php echo $commentcount++;?>
</span>
<div class="comment-avatar">
<?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?>
</div>
<div class="comment-text">
<div class="comment-author">
<?php printf(__('<span>%s</span>'), get_comment_author_link()) ?>
</div>
<div class="comment-date">
<?php echo get_comment_date(); ?>
</div>
<?php if ($comment->comment_approved == '0') : ?>
<em><?php _e('Your comment is awaiting moderation.') ?></em>
<?php endif; ?>

<?php edit_comment_link(__('(Edit)'),' ','') ?>

<?php comment_text() ?>
</div>
<?php
$commentcount++;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum